简体   繁体   English

在调用onDestroy()方法之后,Activity实例是否真的被破坏了?

[英]Is the Activity instance really destroyed after onDestroy() method is called?

I'm confused while learning savedInstanceState Bundle. 我在学习savedInstanceState Bundle时感到困惑。 I found that the Activity instance is not destroyed after onDestroy() method is called, or it can save the data without the help of savedInstanceState Bundle. 我发现,在调用onDestroy()方法之后,Activity实例不会被破坏,或者它可以在不保存saveInstanceState Bundle的情况下保存数据。

This is my test code: 这是我的测试代码:

package com.example.hellotest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private static int testNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_main);
        testNum++;
        Log.d(TAG, "testNum: " + testNum);
        if(savedInstanceState == null)
            Log.d(TAG, "savedInstanceState is null");
        else
            Log.d(TAG, "savedInstanceState is NOT null");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}

And here is the log info: 这是日志信息:

11-17 22:10:14.433: D/MainActivity(23303): onCreate
11-17 22:10:14.463: D/MainActivity(23303): testNum: 1
11-17 22:10:14.463: D/MainActivity(23303): savedInstanceState is null
11-17 22:10:17.527: D/MainActivity(23303): onDestroy
11-17 22:10:18.278: D/MainActivity(23303): onCreate
11-17 22:10:18.298: D/MainActivity(23303): testNum: 2
11-17 22:10:18.298: D/MainActivity(23303): savedInstanceState is null
11-17 22:10:19.569: D/MainActivity(23303): onDestroy
11-17 22:10:20.200: D/MainActivity(23303): onCreate
11-17 22:10:20.220: D/MainActivity(23303): testNum: 3
11-17 22:10:20.220: D/MainActivity(23303): savedInstanceState is null

The testNum never returns to 1 unless I totally kill the process on the phone, which seems to me that the Activity instance is not destroyed really, so we don't need to override the onSaveInstanceState(Bundle outState) method to save the data. 除非我完全杀死电话上的进程,否则testNum永远不会返回1,在我看来,Activity实例并未真正销毁,因此我们无需重写onSaveInstanceState(Bundle outState)方法即可保存数据。

Is there somewhere I'm misunderstanding? 我有误会的地方吗?

testNum is declared as static , and is therefore associated with the class MainActivity , not any particular instance of it. testNum声明为static ,因此与类MainActivity关联,而不是它的任何特定实例。 Unless you need to access testNum statically, you should remove the static identifier: 除非需要静态访问testNum ,否则应删除static标识符:

private int testNum = 0;

Doing this will result in testNum being associated with an instance of the MainActivity class, and will indeed be destroyed. 这样做将导致testNumMainActivity类的实例相关联,并且确实会被销毁。

You can, if you choose, override onSaveInstanceState to maintain instance variables across rotations or if MainActivity is destroyed and recreated for any other configuration changes: 如果选择,可以覆盖onSaveInstanceState以在轮换中维护实例变量,或者如果MainActivity被销毁并为其他任何配置更改而重新创建:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("testNum", testNum);
}

The confusion is between static and instance variables. 静态变量和实例变量之间存在混淆。

Your counter is static, so it has the lifetime of your process rather than any particular instance. 您的计数器是静态的,因此它具有进程的生命周期,而不是任何特定的实例。

Since process lifetime is not guaranteed, you generally don't want to store anything which cannot be trivially recreated there. 由于不能保证进程的生命周期,因此通常您不希望在此存储任何无法重新创建的内容。

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

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