in Android if OS kills my application, will getInt

2019-03-24 08:38发布

问题:

After my application has been restored, of course.

or I have to store everything at onSaveInstanceState()?

回答1:

This conversation seems to answer it: http://groups.google.com/group/android-developers/browse_thread/thread/f47af26b696af2e2. Look for the post starting with "Yes and no" and the few before it.

The answer is in the positive: yes, after restart of an Activity shut down by the OS, getIntent() will return an Intent with the same extras as the original.

Nevertheless, if some Android expert could confirm, I'd be glad to hear that. :)



回答2:

Sobear,

The answer for that is "no, it does not". There are a number of different reasons for this. First, let's rehash how an Activity works (I know you know this, but I am illustrating a point).

  1. In order to start an Activity, an Intent is created. This intent can be from an App Icon on the Home screen, from a component of your App that is currently running, or even from another component of another App provided they have permission to launch it.

  2. The Intent gets sent to Android, and Android tries to resolve it by sending it to an App, or by querying the user if it cannot resolve by itself.

  3. When the App starts, it holds onto the Intent that started it to allow for security checks, back tracking, etc. It allows the developer and user to make sure that this is what they really want to do... This Intent remains throughout the lifecycle of the Activity and anything else that the Activity passes it to.

  4. User does some stuff, gets distracted and moves onto other Activities. The App still continues on until memory is low.

  5. Memory gets low and Android kills the Activity. Activity loses the Intent (unless it was saved somehow).

  6. User sees that the Activity died and decides to restart. Android creates a new Intent to start the Activity -> go back to step 1.

Since a new Intent was required to restart the app, the old intent is no longer keyed to the Activity. As to whether or not it is collected into Garbage, I'm not sure when this happens, but it is not accessible. If storing your Extras is your concern, there is only one way to do this (several possible implementations, of course). The Intent and Extras must be stored in some way. This will not affect the getIntent() function, but you COULD create a personal getOriginalIntent() function to base it on. From there, there are a number of options... For instance, you could restart the Activity with the Original Intent and THEN getIntent() would work it...

FuzzicalLogic



回答3:

Well, you can always do a simple experiment yourself, by turning "don't keep activities" flag on, in developer options.

If that flag is on, whenever you exit the application (say, click the home button), the OS will kill the activity, and when you return to your app, the previous activity will be recreated.

My experiments told me that getIntent() will always have the same data after the recreation of the activity.