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. Is it normal behaviour? Thanks.

  • LocationManager: is the “network” provider always enabled?
  • Conversion from px to dp for Google nexus 7
  • How to place Relative layout at bottom of screen(or linear layout).?
  • Best way to delete Activity and its references in Android Studio
  • Parsing Android menu XML resource to objects list
  • How to set TimePicker show with format 24h
  • Related posts:

    How can I change the OverScroll color in Android 2.3.1?
    Stop Location Listener in Android
    What's the namespace, soap action and url should be for my android app accessing wcf web se...
    Failure Android-L
    What is the purpose of a delegation pattern?
    Uncaught ReferenceError: ionic is not defined
  • How to add floating action button on top of scrollview
  • Android TextureView set background color and show video later
  • Clearing data on account removal
  • Google Play APK and Android Studio APK (USB debug) behaving differently
  • Deep link into an app while the app is already running in the background
  • Is it possible to define a broadcast receiver as an inner class in manifest file?
  • 4 Solutions collect form web for “Is it normal for the “activity.onCreate()” method to be called multiple times”

    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
            }
        }
    

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.