Android 多次调用“activity.onCreate()”方法是否正常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3588682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:52:07  来源:igfitidea点击:

Is it normal for the "activity.onCreate()" method to be called multiple times

androidoncreate

提问by MyName

I have some code in the onCreate method an Activity and noticed that it is being called three times. Is it normal behaviour? Thanks.

我在 Activity 的 onCreate 方法中有一些代码,并注意到它被调用了 3 次。这是正常行为吗?谢谢。

回答by Cheryl Simon

You might want to read through the documentation on the Activity lifecycle.

您可能需要通读有关Activity 生命周期的文档。

OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.

OnCreate 只会在 Activity 的每个生命周期中被调用一次。但是,有许多情况可能会导致您的活动被终止并恢复生机。因此,将再次调用 onCreate。

To support this properly, you can save state information in onSaveInstanceState and restore it fron the state bundle you get in on create.

为了正确支持这一点,您可以将状态信息保存在 onSaveInstanceState 中,并从您在创建时进入的状态包中恢复它。

回答by M. Usman Khan

Other than the expected cases, I have observed that only those activities (onCreate) are called twice which are creating new Thread or Runnable. (I believe this to be a bug in Android).

除了预期的情况外,我观察到只有那些活动 (onCreate) 被调用两次,这些活动正在创建新的 Thread 或 Runnable。(我相信这是 Android 中的一个错误)。

The solution is simple (though you may not like it :p)

解决方案很简单(尽管您可能不喜欢它:p)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        if(savedInstanceState == null){
            // everything else that doesn't update UI
        }
    }

回答by dleal

You can also handle the configuration changes on your own, setting on the AndroidManifest the following statement, in the activity configuration:

您也可以自行处理配置更改,在 AndroidManifest 上设置以下语句,在活动配置中:

android:configChanges="orientation|keyboardHidden"

For further information, you can have a look at the official documentation

有关更多信息,您可以查看官方文档

回答by josiah

The below is a scenario I encountered (and solved) which produces the behavior you are describing:

以下是我遇到(并解决)的场景,它产生了您所描述的行为:

There are 3 events that will trigger OnTouch - 1. android.view.MotionEvent.ACTION_UP 2. android.view.MotionEvent.ACTION_DOWN 3. android.view.MotionEvent.ACTION_MOVE.

有 3 个事件会触发 OnTouch - 1. android.view.MotionEvent.ACTION_UP 2. android.view.MotionEvent.ACTION_DOWN 3. android.view.MotionEvent.ACTION_MOVE。

Often, all three of these events fire at the same time to trigger the OnTouch listener. When this listener is used to launch an activity (via an Intent passed to startActivity()), you can reproduce this behavior which would call OnCreate on the Activity multiple times (3 in this example).

通常,所有这三个事件同时触发以触发 OnTouch 侦听器。当此侦听器用于启动活动(通过传递给 startActivity() 的 Intent)时,您可以重现此行为,该行为将多次调用 Activity 上的 OnCreate(在此示例中为 3)。

If it's not this listener type you are using to start the activity, you may want to look into the documentation for whatever listener is triggering your activity to see if you are experiencing a similar scenario. Chances are that not just one event triggers the listener.

如果不是您用来启动 Activity 的侦听器类型,您可能需要查看触发您的 Activity 的侦听器的文档,看看您是否遇到了类似的情况。很有可能不仅仅是一个事件触发了监听器。

回答by Mike

I had a similar problem, it was caused by MobileAds. After I initialized them BEFORE super.onCreate(...)the problem was gone.

我有一个类似的问题,它是由 MobileAds 引起的。在我初始化它们之后super.onCreate(...),问题就消失了。

回答by Saman Sattari

In Some cases it might be because of logging multiple times. Run your application in debugging mode and check if your code runs twice or its just logging multiple times.

在某些情况下,这可能是因为多次记录。在调试模式下运行您的应用程序,并检查您的代码是运行了两次还是只是记录了多次。

If its just logging check my answer in this question: Logcat showing information 3 times on AVD

如果它只是记录,请检查我在这个问题中的答案: Logcat 在 AVD 上显示信息 3 次

回答by Grant Park

This can also occur if you have in Developer Settings "Don't Leave Activities" turned on.

如果您在开发人员设置中打开了“不要离开活动”,也会发生这种情况。