“activity.onCreate()”是否正常?方法被多次调用
我在 Activity 的 onCreate 方法中有一些代码,并注意到它被调用了三次。这是正常行为吗?谢谢。
I have some code in the onCreate method an Activity and noticed that it is being called three times. Is it normal behaviour? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
除了预期的情况之外,我观察到只有那些创建新线程或可运行的活动(onCreate)被调用两次。 (我相信这是 Android 中的一个错误)。
解决方案很简单(尽管你可能不喜欢它:p)
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).
The solution is simple (though you may not like it :p)
您可能需要阅读有关活动生命周期的文档。
OnCreate 在 Activity 的每个生命周期中只会被调用一次。然而,有很多情况可能会导致您的 Activity 被终止或恢复。这样,onCreate就会被再次调用。
为了正确支持这一点,您可以在 onSaveInstanceState 中保存状态信息,并从创建时获得的状态包中恢复它。
You might want to read through the documentation on the Activity lifecycle.
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.
To support this properly, you can save state information in onSaveInstanceState and restore it fron the state bundle you get in on create.
就我而言,子类的
onCreate
方法运行了两次。调用超类的 onCreate 方法后更改主题会导致此问题。我在超类的onCreate
方法之前设置了主题,然后子类的onCreate
方法没有再次调用。转换成这样:
In my case,
onCreate
method of the subclass is running twice. Changing theme afteronCreate
method of the superclass is called is causing this. I set theme beforeonCreate
method of superclass thenonCreate
method of subclass was not called again.Converted to this:
就我而言,它在 onCreate 之后调用 setDefaultNightMode:
这可以修复它:
In my case it was calling setDefaultNightMode after onCreate:
this fixes it:
我也遇到过类似的问题,是 MobileAds 造成的。
在我在
super.onCreate(...)
之前初始化它们之后,问题就消失了。I had a similar problem, it was caused by MobileAds.
After I initialized them BEFORE
super.onCreate(...)
the problem was gone.如果您在开发者设置中打开了“不要离开活动”,也可能会发生这种情况。
This can also occur if you have in Developer Settings "Don't Leave Activities" turned on.
您还可以自行处理配置更改,在 Activity 配置中的 AndroidManifest 上设置以下语句:
有关详细信息,您可以查看 官方文档
You can also handle the configuration changes on your own, setting on the AndroidManifest the following statement, in the activity configuration:
For further information, you can have a look at the official documentation
下面是我遇到(并解决)的一个场景,它会产生您所描述的行为:
有 3 个事件将触发 OnTouch - 1. android.view.MotionEvent.ACTION_UP 2. android.view.MotionEvent.ACTION_DOWN 3. android.view.MotionEvent.ACTION_UP 2. android.view.MotionEvent.ACTION_DOWN view.MotionEvent.ACTION_MOVE。
通常,所有这三个事件都会同时触发以触发 OnTouch 侦听器。当此侦听器用于启动活动(通过传递给 startActivity() 的 Intent)时,您可以重现此行为,这将多次调用 Activity 上的 OnCreate(本例中为 3 次)。
如果您用来启动活动的不是此侦听器类型,您可能需要查看文档以了解触发您的活动的任何侦听器,以了解您是否遇到类似的情况。很可能不仅仅是一个事件触发侦听器。
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.
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).
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.
在某些情况下,这可能是因为多次记录。在调试模式下运行您的应用程序,并检查您的代码是否运行两次或只是记录多次。
如果只是记录,请检查我在这个问题中的答案:
Logcat 在 AVD 上显示信息 3 次
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
我刚刚遇到了这个问题,读完所有这些后,没有任何帮助。这对我有帮助。
MainLauncher = true
添加到您的MainActivity.cs
类中。I just had this issue and after reading all this, nothing helped. Here is what helped me.
MainLauncher = true
to yourMainActivity.cs
class.