Inx
Inx

Reputation: 2384

Store a string until the application gets "force stopped"

I need a way to store a json-string as for as long as the application is alive.

So to put this into other words :) If the application changes orientation or gets paused or stopped i need to store my value/string.. But if the application gets forced killed for some reason I want the value to be removed/droped

Im trying to create a custom history-handler.. and this history handler is suppoed to keep history of actions with in my application for as long as the application process is actually running, but as soon as the process gets killed I need the history to get erased as well.

I have tried to store the value/string as a SharedPrefernce, and to simply remove it once isFinishing()== true I have also tried using the onDestroy as well.. but there seems to be no "beforeProcessKilled"-event to listen for.. so maybe there is another option than using the SharedPreferences?

Thanks in advance!

Upvotes: 0

Views: 275

Answers (3)

romo
romo

Reputation: 1990

I usually do something like this

public class Globals extends Application {

    private String jsonString;

    public void jsonString(String string) {
        jsonString = string;
    }

    public String jsonString() {
        return jsonString;
    }

}

then access it with something like

Globals myGlobal = (Globals)getApplication();
// get json string
String json = myGlobal.jsonString();

so on...

Upvotes: 0

Adrien Cerdan
Adrien Cerdan

Reputation: 1015

It's my way, maybe not the best. You can create a class named "Global" or other... Then declare inside something like this, some "static" variables :

public class Global_variables {

public static JSONArray test;
public static String test1;

}

Then, in your activity or other, import "Global_variables" and get your String or other like this:

String IwantTostore = Global_variable.test1;

If you change orientation, your Global_variable.test1 keep alive ;)

Hope this help

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007369

Use a simple Java static data member. That value will live until the process is terminated, for whatever reason.

Upvotes: 2

Related Questions