简体   繁体   English

在应用程序被杀之后,'Bundle savedInstanceState'是否会存活?

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

Will savedInstanceState bundle in onCreate() method be alive (not null) after Application is being killed? 应用程序被杀后, onCreate()方法中的savedInstanceState包是否存活(非空)? If it would, where this bundle is stored in the system. 如果可以的话,该捆绑包存储在系统中。

If Android kills the process hosting your app, it still maintains the "saved instance state" of all active (non-finished) activities. 如果Android杀死托管您应用的进程,它仍会维护所有活动(未完成)活动的“已保存实例状态”。 This data is stored by the ActivityManager . 该数据由ActivityManager存储。 If the user returns to your application, Android will create a new process for the app, instantiate the Application instance again and then create an instance of the top activity in the activity stack . 如果用户返回到您的应用程序,Android将为应用程序创建一个新进程,再次实例化Application实例,然后在活动堆栈中创建顶级活动的实例。 It will then call onCreate() on that activity instance passing it the "saved instance state" that was most recently saved for that activity. 然后,它将在该活动实例上调用onCreate() ,并将最近为该活动保存的“已保存实例状态”传递给它。

If you reboot your phone, all this data is lost (Android does not save application state across reboots). 如果重新启动手机,则所有这些数据都将丢失(Android不会在重新启动后保存应用程序状态)。

No it will not, android app maintain it's state as long as it is running : (foreground and background). 不,它不会,Android应用程序维持它的状态,只要它运行:(前景和背景)。

if you are looking for something that can span application's different life-cycle use something like SharedPreferences . 如果您正在寻找可以跨越应用程序的不同生命周期的东西,请使用像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. 这只发生在android需要内存并且必须终止你的活动以释放活动堆栈中的资源和活动时。 its documented that it is a convenience way to maintain user experience. 它记录了它是一种维护用户体验的便利方式。

android documentations: android文档:

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) 如果需要杀死它的进程,当用户导航回活动(再次在屏幕上显示)时,将使用之前在onSaveInstanceState中提供的savedInstanceState调用其onCreate(android.os.Bundle)方法(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. 所以你不应该期望一直维护InstanceState。 Activity source code at codegrep codegrep上的活动源代码

Edit 编辑

by searching google for android instance state I came by this resource Android Recreating an Activity 通过搜索谷歌的android instance state我来到这个资源Android重新创建一个活动

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. 当您的活动因用户按下Back或活动自行完成而被销毁时,系统对该Activity实例的概念将永远消失,因为该行为表明不再需要该活动。 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. 但是,如果系统由于系统约束(而不是正常的应用程序行为)而破坏活动,那么虽然实际的Activity实例已经消失,但系统会记住它存在,如果用户导航回它,系统会创建一个新的活动的实例,使用一组保存的数据来描述销毁时的活动状态。 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. 系统用于恢复先前状态的已保存数据称为“实例状态”,是存储在Bundle对象中的键值对的集合。

Edit 2 After digging around android internals, it seems like it is all about ActivityManagerNative 编辑2在挖掘android内部结构之后,似乎它就是关于ActivityManagerNative的

Whenever an activity is pausing it's state is passed Using a Parcel Object to the ActivityManager process. 每当活动暂停时,将状态传递给使用Parcel对象到ActivityManager进程。

    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 每当ActivityManagerNative创建一个活动时,它就会使用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;
    }
   .....

Following table from one of the Android training materials might be more helpful. 从其中一个Android培训材料的下表可能会更有帮助。

In that, bundle saved by onSavedInstanceState is valid while the app is OPEN which means the app may be PAUSed or even STOPed and DESTROYed (due to reasons such as rotation) but NOT QUIT using the Back button. 其中, onSavedInstanceState保存的包在onSavedInstanceState为OPEN时有效,这意味着应用程序可能被PAUSed甚至停止和DESTROYed(由于诸如旋转等原因)但使用Back按钮不退出。

The table also shows other available options if not onSavedInstanceState . 如果不是onSavedInstanceState该表还显示其他可用选项。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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