简体   繁体   English

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

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

I have some code in the onCreate method an Activity and noticed that it is being called three times.我在 Activity 的 onCreate 方法中有一些代码,并注意到它被调用了 3 次。 Is it normal behaviour?这是正常行为吗? Thanks.谢谢。

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. OnCreate 只会在 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。

To support this properly, you can save state information in onSaveInstanceState and restore it fron the state bundle you get in on create.为了正确支持这一点,您可以将状态信息保存在 onSaveInstanceState 中,并从您在创建时进入的状态包中恢复它。

Other than the expected cases, I have observed that only those activities (onCreate) are called twice which are creating new Thread or Runnable.除了预期的情况外,我观察到只有那些活动 (onCreate) 被调用两次,这些活动正在创建新的 Thread 或 Runnable。 (I believe this to be a bug in Android). (我相信这是 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
        }
    }

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

In my case, onCreate method of the subclass is running twice.就我而言,子类的onCreate方法运行了两次。 Changing theme after onCreate method of the superclass is called is causing this.在调用超类的onCreate方法后更改主题会导致这种情况。 I set theme before onCreate method of superclass then onCreate method of subclass was not called again.我之前设置主题onCreate超则方法onCreate子类的方法不会再次调用。

    public class XActivity extends YActivity { // XActivity is subclass

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

    public class YActivity extends AppCompatActivity { //YActivity is superclass.

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme();
    }
    ...

Converted to this:转换成这样:

    public class YActivity extends AppCompatActivity { // YActivity is superclass.

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme();
        super.onCreate(savedInstanceState);
    }
    ...

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有关更多信息,您可以查看官方文档

I had a similar problem, it was caused by MobileAds.我有一个类似的问题,它是由 MobileAds 引起的。 After I initialized them BEFORE super.onCreate(...) the problem was gone.在我在super.onCreate(...)之前初始化它们之后,问题就消失了。

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.通常,所有这三个事件同时触发以触发 OnTouch 侦听器。 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).当此侦听器用于启动活动(通过传递给 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.如果不是您用来启动 Activity 的侦听器类型,您可能需要查看触发您的 Activity 的侦听器的文档,看看您是否遇到了类似的情况。 Chances are that not just one event triggers the listener.很有可能不仅仅是一个事件触发了监听器。

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 次

I just had this issue and after reading all this, nothing helped.我刚刚遇到了这个问题,在阅读了所有这些之后,没有任何帮助。 Here is what helped me.这对我有帮助。

  • Add the attribute MainLauncher = true to your MainActivity.cs class.将属性MainLauncher = true添加到MainActivity.cs class。

In my case it was calling setDefaultNightMode after onCreate :就我而言,它在onCreate之后调用setDefaultNightMode

super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

this fixes it:这解决了它:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
super.onCreate(savedInstanceState);

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

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