Will 'Bundle savedInstanceState' be alive after Application is being killed?

前端 未结 3 1094
抹茶落季
抹茶落季 2020-12-09 11:41

Will savedInstanceState bundle in onCreate() method be alive (not null) after Application is being killed? If it would, where this bundle is stored

3条回答
  •  被撕碎了的回忆
    2020-12-09 12:08

    No it will not, android app maintain it's state as long as it is running : (foreground and background).

    if you are looking for something that can span application's different life-cycle use something like SharedPreferences .

    Regarding

    if the system kills your application process and the user navigates back to your activity

    This only happens when android needs memory and have to kill your activity to free resources and your activity in the activity stack. its documented that it is a convenience way to maintain user experience.

    android documentations:

    A background activity (an activity that is not visible to the user and has been paused) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(android.os.Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(android.os.Bundle)

    so that it can restart itself in the same state as the user last left it.

    so you should not expect the InstanceState be maintained all of the time. Activity source code at codegrep

    Edit

    by searching google for android instance state I came by this resource Android Recreating an Activity

    When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

    Edit 2 After digging around android internals, it seems like it is all about ActivityManagerNative

    Whenever an activity is pausing it's state is passed Using a 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 ActivityManagerNative creates an activity, it passes that State back to the activity 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;
        }
       .....
    

提交回复
热议问题