Question

My app is made of two activities, A and B. I'm considering this sequence of steps:

  • Activity A is started.
  • A launches B [A is paused, B is running].
  • B launches a map intent [A and B are both paused now].

Now the user is using the maps application and the system decides it needs more memory. Can the system kill only one of my activities for memory, or will it always kill all activities in a "process" in this situation?

Both activities share some static data like:

class Data {
    public static String mName;
    public void save() {
      // write to file: mName;
    }
    public void load() {
      // mName = read from file;
    }
}

ActivityA.mTextView.setText(Data.mName);
ActivityB.mListView.addText(Data.mName);

so when any activity in my app gets onSaveInstanceBundleSate() called, I call Data.save() to write it to disk. Now the question is, in an Activity's onCreate() method, should I simply check to see if Data.mName == null, and if so, assume the Activity is returning from a kill state, and try restoring from disk again? I'm unclear when this restoring should be done, considering Activity A may or may not still be alive etc. - and I don't want to corrupt state if Activity A is still alive but B is coming back from a kill state,

Thanks

Thanks

Was it helpful?

Solution

Probably the best solution is to move your static data to a Service. That way the data can be saved and restored when android shuts down the Service rather than when Android shuts down either of the individual activities using the data.

Without using a Service (or alternately a Content Provider, or even overriding Application although that seems to be frowned upon), you have to manage that lifecycle yourself, which as you've seen can be tricky.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top