Search code examples
androidactivity-lifecycle

How is bundle sent to onCreate if App process killed?


In the Activity Lifecycle diagram there's an arrow from 'onStop' to 'App process killed' to 'onCreate'. I've always wondered, and now I'm going to give a small talk on fragments; How is it possible for onCreate to receive the bundle from onStop if the whole app process is destroyed? Does the system keep track of killed apps and their activity bundles? I think that would be how it would do it because at that point the killed application would have zero memory allocated to it.

Also, from the last paragraph on the page Managing the Activity Lifecycle>Starting an Activity, "The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one:...' And that one situation isn't stated as being low on memory. This makes me think that the arrow should never go from onStop to onCreate because of 'Apps with higher priority need memory'. Is this a typo or am I reading it wrong? I assume I'm reading it wrong because of the 'Killable?' column in 'in general the movement through an activity's lifecycle looks like this:' chart.

One of these has to be wrong either the arrow in the activity lifecycle chart or the "The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one:..." statement. Hopefully I'm reading out of context.


Solution

  • How is it possible for onCreate to receive the bundle from onStop if the whole app process is destroyed?

    It doesn't "receive the bundle from onStop", insofar as onStop() has nothing to do with a Bundle. The Bundle delivered to onCreate() and onRestoreInstanceState() contains the data put in an earlier Bundle by onSaveInstanceState(). The content of that Bundle is passed across process boundaries to a core OS process that manages the state of outstanding activities and their tasks. That content is passed back to the fresh process for your app if and when it is relevant.

    Does the system keep track of killed apps and their activity bundles?

    The OS keeps track of outstanding tasks. For a while (~30 minutes since last use), it keeps track of the instance state Bundle for the activities on the task.

    The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one

    There is more than one situation in which onDestroy() is not called. Terminating the process due to low memory conditions may or may not result in onDestroy() being called, depending on the urgency of the need for system RAM.