6

I think my ideas on activity lifecycle and bundles
are a little confused,can you help me?

Let's suppose the user opens activity A from home screen,
activity A "calls" activity B which fills the screen.
On this event onSaveInstanceState() is called on activity A then onPause() and onStop().
Since there are too many apps currently running on the system,
andorid decides to kill the process hosting activity A.

When the user navigates back to activity A,onCreate() is called an we can
use the bundle ( setted during the last call of onSaveInstaceStae() ) to restore the state. then onStart(),onRestoreInsanceState()
and onResume() are called,
am I right?

Then lets suppose the user presses back key to exit from activity A
onPause(),onStop() and onDestory() are called in sequence on activity A (the call of onDestroy() could be postponed though) onSaveInsanceState() should not be called in this scenario.

When the user opens again activity A later on then the bundle
passed to onCreate() is null,right?

Now Suppose the user rotates screen onSaveInsanceState() ,OnPause() ,OnStop(), OnDestroy() are called
then onCreate() with bundle setted by the last call to onSaveInsanceState(),
and then onStart(), and onRestore(). am I right?

My guess is that:

when the user creates an ativity,the bundle passed to onCreate() is always null and onRestoreState() is never called,but when the system creates it , for instance when it killed the activity because of low memory or because of a rotation event,the bundle passed is the one setted by the last call of onSaveInstanceState().

Is my guess right?

thanks and sorry for my poor english.

P.S. : I think onRestoreInstanceState() is passed the same bundle is passed onCreate() but typically state is restored using onCreate().

GionJh
  • 2,742
  • 2
  • 29
  • 68
  • 2
    You may have to wait several days to get a good answer, not a few minutes.. – Mangusto Nov 18 '11 at 11:43
  • I'm an android beginner,didn't Know my question was so hard... – GionJh Nov 18 '11 at 11:46
  • You talk about Activities calling another Activity, but, it you add flags to the intent like "Intent.FLAG_ACTIVITY_REORDER_TO_FRONT" the onCreate method will not be called if the Activity was not killed by the system – SERPRO Nov 18 '11 at 12:08
  • @SeRPRo My question is about android standard behaviour,not special cases. – GionJh Nov 18 '11 at 12:11
  • @user986437 OK I thought you were having troubles with your code, not a general programming question. – SERPRO Nov 18 '11 at 12:15
  • Check [this answer related to activity life cycle](http://stackoverflow.com/a/8516056/265167), this may help you i guess! – Yaqub Ahmad Dec 15 '11 at 07:49
  • @user986437 we are seeing the same problem where the state is never restored correctly. tried different permutations but no luck and so tend to agree with you. how did you resolve the problem? – Henry Thornton Apr 08 '12 at 07:37

3 Answers3

0

onRestoreInstanceState() is passed the same bundle is passed onCreate() is right, and the system restart the activity by call onCreate() and also call onRestoreInstanceState(),the bundle will be null if get from onCreate() when the activity first started.

0

Since there are too many apps currently running on the system, andorid decides to kill the process hosting activity A.

This is a very common situation. Furthermore - you can emulate this action using developers option.

enter image description here

In this case each activity, that was move into background, will be automatically destroyed.

About Bundle in OnCreate .

I can get from my memory only two cases when OnCreate will be called with non-null Bundle. First - described above. Second case - screen rotation.

When you launch app Android calles

  • onCreate
  • onStart
  • onResume

After that lets doing screen rotation. Android will call

  • onSaveInstanceState
  • onPause
  • onStop
  • onDestroy
  • onCreate
  • onStart
  • onRestoreInstanceState
  • onResume

The more information you can find on topic about recreating

Community
  • 1
  • 1
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
0

Interesting question - never thought about it.

Take a look at the documentation, onCreate() and onSaveInstanceState().

This answers at least your question what Bundle will be supplied to onCreate().

Unfortunately there is no exact definition on which events onSaveInstanceState() is called, but I guess it is called by default in all relevant situations (whatever they may be...), but you can find out for some situations (e.g. rotating the screen) by putting a Log.i() to LogCat.

Philipp Wendt
  • 2,538
  • 1
  • 15
  • 17