Will the "Bundle savedInstanceState" be alive after the application is killed?

Will the savedInstanceState method in onCreate() launched (not null) after the application is killed? If it were, where this package is stored on the system.

+8
android
source share
3 answers

If Android kills the hosting process of your application, it still saves the โ€œsaved instance stateโ€ of all active (incomplete) actions. This data is saved using the ActivityManager . If the user returns to your application, Android will create a new process for the application, create an instance of Application again, and then create an instance of the top action in the action stack . It will then call onCreate() on this activity instance, passing it the "saved instance state" that was recently saved for this action.

If you restart your phone, all this data will be lost (Android does not save the state of the application when you restart).

+14
source share

No, it will not, the Android application maintains its state while it works: (front and rear).

if you are looking for something that can span applications, different life cycles use something like SharedPreferences.

Relatively

if the system kills your application process and the user goes to your activity

This happens only when the android needs memory and must kill your activity in order to free up resources and your activity in the action stack. it has documented that this is a convenient way to support the user interface.

android documentation:

Background activity (activity that is not visible to the user and paused) is no longer critical, so the system can safely kill its process to restore memory for other front or visible processes. If his process should be killed when the user goes to action (which again becomes visible on the screen), his onCreate (android.os.Bundle) method will be called with the saved InstanceState, which was previously included in onSaveInstanceState (android .os.Bundle)

so that he can restart himself in the same state in which the user left him.

therefore, you should not expect InstanceState to be supported all the time. The source code of the operation in codegrep

Edit

by google search for android instance state I came to this Android resource recreating activity

When your activity is destroyed because the user clicks "Back" or the action ends, the system concept of the Activity instance disappears forever because the behavior indicates that the action is no longer required. However, if the system destroys the activity due to system restrictions (and not the usual behavior of the application), then although the actual instance of the Activity is absent, the system remembers that it existed so that if the user navigates to it, the system creates a new instance of the action using the set stored data, which describes the state of activity when it is destroyed. The stored data that the system uses to restore the previous state is called the "instance state" and is a set of key-value pairs stored in the Bundle object.

Edit 2 After digging around Android's internal elements, it seems like it's all about ActivityManagerNative

Whenever an action is paused, the state is passed using the Parcel object to the ActivityManager process.

  public void activityPaused(IBinder token, Bundle state) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(IActivityManager.descriptor); data.writeStrongBinder(token); data.writeBundle(state); mRemote.transact(ACTIVITY_PAUSED_TRANSACTION, data, reply, 0); reply.readException(); data.recycle(); reply.recycle(); } 

And whenever an ActivityManagerNative creates an action, it passes that state back to the action using Parcel

  public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code) { case START_ACTIVITY_TRANSACTION: { data.enforceInterface(IActivityManager.descriptor); IBinder b = data.readStrongBinder(); IApplicationThread app = ApplicationThreadNative.asInterface(b); Intent intent = Intent.CREATOR.createFromParcel(data); String resolvedType = data.readString(); Uri[] grantedUriPermissions = data.createTypedArray(Uri.CREATOR); int grantedMode = data.readInt(); IBinder resultTo = data.readStrongBinder(); String resultWho = data.readString(); int requestCode = data.readInt(); boolean onlyIfNeeded = data.readInt() != 0; boolean debug = data.readInt() != 0; int result = startActivity(app, intent, resolvedType, grantedUriPermissions, grantedMode, resultTo, resultWho, requestCode, onlyIfNeeded, debug); reply.writeNoException(); reply.writeInt(result); return true; } ..... 
+6
source share

The following table from one of the Android training materials may be more useful.

In this case, the package saved using onSavedInstanceState is valid when the application is open, which means that the application may be PAUSED or even STOPed and DESTROYed (due to reasons such as rotation), but NOT QUIT using the back button.

The table also shows other available options, if not onSavedInstanceState .

enter image description here

0
source share

All Articles