1

My app an activity (which now a subclass on FragmentActivity, although I don't think that matters), let's call it Activity A.

In it, a button navigates away to (say) Activity B by starting it via Intent using startActivity() with no special flags.

Neither activity has any special flags (SingleTop) etc in the manifest, not calls finish() etc. i.e. nothing unusual.

Activity A's method onSaveInstanceState() get's called and I save some state info.

In Activity B I hit the BACK key and come back to Activity A.

It's onCreate() method is called, but the Bundle of "savedInstanceState" is null, and so I cannot reconstruct the state I had previously saved.

Any ideas what I am doing wrong, and how I can ensure I get the state back.

BTW: On a configuration change (say rotate), it all works fine....

Andrew Mackenzie
  • 5,477
  • 5
  • 48
  • 70
  • Starting Activity B and returning to Activity A should only call Activity A's `onPause()` and `onResume()` methods. Your previous state shouldn't be altered... unless the OS killed A in the background to reclaim memory, but then this would be no different than an orientation change. – Sam Sep 05 '12 at 16:02

2 Answers2

1

It seems strange that onCreate() is getting called when you go back to Activity A just by pressing the back key. In general it should just show the existing activity without trying to recreate it. I also think it also strange that onSaveInstanceState() is getting called when you start the other activity. In fact, the documentation states that it probably won't call onSaveInstanceState() when starting Activity B:

An example: when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

From here.

I think something else is going on.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • Yes, I also couldn't understand that onCreate() was being called again and you confirmed it....so I investigated....turns out, unbeknownst-to-me, the Activity B was capturing the BACK key and starting a new instance of Activity A (a long story as to why it does that.....). FIXED, thanks – Andrew Mackenzie Sep 06 '12 at 13:12
0

Activity A was never destroyed, therefore onCreate() should not be called. Can you please put some code up?

Parth Mehrotra
  • 2,712
  • 1
  • 23
  • 31