0

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!

Inx
  • 2,364
  • 7
  • 38
  • 55

3 Answers3

2

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • like a normal static variable/objectref ? (like static String test = "test"?) Thought I tried that.. but for some reason that lost the value as well as soon as I changed the orientation of my device.. but I might be wrong.. – Inx Jul 02 '13 at 15:09
  • @Inx: "like a normal static variable/objectref ?" -- yes. "but for some reason that lost the value as well as soon as I changed the orientation of my device" -- only if you accidentally reset the value yourself. – CommonsWare Jul 02 '13 at 15:44
  • ok thank you! I will try this again and see if there is anything else causing the "reset" – Inx Jul 03 '13 at 07:23
0

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

Adrien Cerdan
  • 1,005
  • 1
  • 11
  • 21
0

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...

romo
  • 1,990
  • 11
  • 10