Search code examples
androidexpandablelistviewactivity-finish

state of activity after startActivityForResult


In the android documentantion said this:

...when an activity is paused or stopped, the state of the activity is retained. This is true because the Activity object is still held in memory when it is paused or stopped—all information about its members and current state is still alive. Thus, any changes the user made within the activity are retained so that when the activity returns to the foreground (when it "resumes"), those changes are still there.

But in my Activity, when I come back from other activity the expandablelist is reset (all the expanded groups are collapsed). The data is not modified and the adapter and cursor are not notify or requery. I've checked the onDestroy callback method is never called, only onPause and onStop.

I go out of the activity using startActivityForResult and I come back using finish().

Please, why I am losing the state? Thanks.


Solution

  • Many times when you use startActivityForResult, you lose your state. You need to save all pertinent data by overriding the onSaveInstanceState and adding data to the Bundle. Also override onRestoreInstanceState to handle when you come back and need to restore your data. So in your case you might add to the Bundle which parts of the expandablelist are expanding so that you can expand them again when you return.