简体   繁体   中英

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.

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.

It is very possible for onCreate to be called after onStop(). You pass bundles using onSaveInstanceState() which is called anytime the activity or fragment pauses or stops. Say you have an activity and press home. OnStop and onSaveInstanceState are both called. In onSaveInstanceState you save your bundle to save the state of the app. The app is then killed because it was in the background for too long. Then when you open the app back up the bundle from onSaveInstanceState is passed to oncreate in the SavedInstanceState parameter when ever it gets recreated. See the official docs for more https://developer.android.com/training/basics/activity-lifecycle/recreating.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM