开发者

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

I have some code in the onCreate method an Activit开发者_高级运维y and noticed that it is being called three times. Is it normal behaviour? Thanks.


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.


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)

@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. Changing theme after onCreate method of the superclass is called is causing this. I set theme before onCreate method of superclass then onCreate method of subclass was not called again.

    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);
    }
    ...


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.


In my case it was calling setDefaultNightMode after onCreate:

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

this fixes it:

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


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

android:configChanges="orientation|keyboardHidden"

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


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.


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


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜