1

I've recently started Android programming and have run into many problems, but have been able to solve all except this one. For the sake of simplicity, I'll try to sum up what is happening.

  • I have a MainActivity, which has a static ArrayList
  • I go into a sub-activity, where I change the value of the ArrayList of MainActivity
  • I leave the sub-activity, entering MainActivity
  • I check the value of this ArrayList, and it now contains null values

Putting Logcat statements before and after the super.oncreate() call in MainActivity.onCreate(), I can see that some part of super.onCreate() changes the previous correct values in the ArrayList to null.

In case it is important, this static ArrayList contains custom (serializable) objects, each of which contains an ArrayList of custom (serializable) objects whose values themselves are null.

If that's confusing, I'll try to represent it in pseudo-code:

static ArrayList<CustomObject>

CustomObject{ 
      ArrayList<OtherCustomObject> //values of this ArrayList become null
}

Of course if more information is needed, I will try to provide as much as I can.

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
Jake Moritz
  • 843
  • 2
  • 9
  • 21
  • This may help http://stackoverflow.com/q/151777/2556111 – ramaral Dec 29 '13 at 22:46
  • Do you have more than one thread involved? (need to use volatile in that case). Please post some of the code that changes the values and how you start the activity. – Steve M Dec 29 '13 at 22:46
  • @ramaral he doesn't need to save activity state, this is a static variable. – Steve M Dec 29 '13 at 22:46
  • Another idea is that the whole process is being destroyed and restarted for some reason, but there is no way of knowing from what's posted. – Steve M Dec 29 '13 at 22:49
  • Well it should work just fine if you don't explicitly set those values to null maybe when the other activity goes to onDestroy() .. the activities code would help us very much on this, otherwise I don't think we can help too much.. – Cata Dec 29 '13 at 22:52
  • One debug suggestion would to subclass Application and @Override onCreate() and place a Log statement there to see if the process has been recreated. – Steve M Dec 29 '13 at 22:54

2 Answers2

2

Regardless of why this happening in your case, storing a list like this in a static variable and expecting it to be there (with no plan to recreate it) is not a good idea since the system could decide to kill your process when it's low on memory. When the user attempts to reenter your app, you will have null values as the system tries to recreate the activity.

If the data is important, write it out to a file (you said it was already Serializable) and read it when you start the main activity.

Steve M
  • 9,296
  • 11
  • 49
  • 98
  • Agreed. Except when you say "write it out to a file"—wouldn't the "outState" Bundle in [onSaveInstanceState()](http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState) be the most natural option? – Jonik Dec 29 '13 at 23:22
  • 1
    Depends on if the data needs to persist even if the activity is exited by the user (or phone is restarted, etc.) or not. The outState Bundle will persist if the system destroys and recreates process but not in the cases I just mentioned. – Steve M Dec 29 '13 at 23:37
1

Static data lives until either

  1. the VM shuts down,
  2. the process terminates, or
  3. the class is unloaded.

None of these are happening in your described case. The VM is up, process is running and Dalvik doesn't unload classes. Please provide additional info to repro the issue.

laalto
  • 150,114
  • 66
  • 286
  • 303