Activity lifecycle - onCreate called on every re-orientation

喜你入骨 提交于 2019-11-28 03:47:25

Activity is recreated after each rotation by default. You can override this behaviour with configChanges attribute of the activity tag in AndroidManifest. For further details and different options, see http://developer.android.com/guide/topics/resources/runtime-changes.html

achellies
android:configChanges="keyboardHidden|orientation|screenSize"

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

http://developer.android.com/guide/topics/resources/runtime-changes.html

What happen when orientation changed

Life Cycle of orientation

onPause();
onSaveInstanceState();
onStop();
onDestroy();

onCreate();
onStart();
onResume();

---- app recreated and now is running ---

If you do long operation in onCreate() and want prevent re-create your activity add configChanges attribute in your mainfest

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden|screenSize"
          android:label="@string/app_name">

screenSize if you targeting api >= 13

Actvity Lifecycle when you rotate screen

onPause
onSaveInstanceState
onStop
onDestroy

onCreate
onStart
onRestoreInstanceState
onResume
josephus

If you want to prevent FC from not enough memory, you need to deallocate resources in onStop() or onPause(). this allows you to use fresh memory in onCreate().

This is an alternate solution to preventing the recreation of the activity by using

android:configChanges="keyboardHidden|orientation"

As sometimes your activity's layout is different in portrait and landscape (layout, layout-land). preventing recreate on orientation change will prevent your activity from using the other orientation's layout.

On Create method will call everytime when you do orientation, to avoid this you have to use

//Define Below in you Manifest file.
           <activity
                  android:name="com.ecordia.activities.evidence.MediaAttachmentView"
                  android:configChanges="keyboardHidden|orientation|screenSize"
            </activity>

//Define Below in your activity. 

         @Override
            public void onConfigurationChanged(Configuration newConfig) {

              super.onConfigurationChanged(newConfig);

              if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                  //your code
              } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //your code

              }
            }

It will works like a charm!!

Yes, activity's onCreate() is called everytime when the orientation changes but you can avoid the re-creation of Activity by adding configChanges attribute of Activity in your AndroidManifest file in the activity tag.

android:configChanges="keyboardHidden|orientation"

One of the most common and suggested “solutions” to dealing with orientation changes is to not deal with them. You can do this by setting the android:configChanges flag on your Activity in AndroidManifest.xml as shown below:

<activity
    android:name=".MyActivity"
    android:label="@string/title_my_activity"
    android:configChanges="orientation|screenSize|keyboardHidden" />

This is NOT the correct way to deal with orientation changes.

CORRECT way is to implement the onSaveInstanceState method (this could be in your Activity, Fragment or both) and place the values you need to save in the Bundle argument that gets passed to the method.

It is nicely described here: http://code.hootsuite.com/orientation-changes-on-android/

While it may seem a bit tedious to implement, handling orientation changes properly provides you with several benefits: you will be able to easily use alternate layouts in portrait and landscape orientations, and you will be able to handle many exceptional states such as low memory situations and interruptions from incoming phone calls without any extra code.

Himanshu Virmani

Use

android:configChanges="keyboardHidden|orientation" in Manifest XML activity Tag

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
}

Use the above code to perform changes related to orientation in your Activity Java Code

Cheers!!!

animesh shrivastava

Kindly see my way of doing it:-

http://animeshrivastava.blogspot.in/2017/08/activity-lifecycle-oncreate-beating_3.html

snippet is:-

@Override
    protected void onSaveInstanceState(Bundle b)
    {
            super.onSaveInstanceState(b);
        String str="Screen Change="+String.valueOf(screenChange)+"....";
            Toast.makeText(ctx,str+"You are changing orientation...",Toast.LENGTH_SHORT).show();
        screenChange=true;

    }

    @Override
    public void onCreate(Bundle b)
        {
        super.onCreate(b);
        ctx=getApplicationContext();
        if(!screenChange)
        {
             String str="Screen Change="+String.valueOf(screenChange);

I had the same problem, in which my onCreate is called multiple times when the screen orientation is changed. My problem got solved when i add android:configChanges="orientation|keyboardHidden|screenSize" in the activity tag in manifest

I had the same problem and I did some workaround

Define didLoad boolean variable with false value

private boolean didLoad = false;

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

    if (!this.didLoad){
        // Your code...
        this.didLoad = true;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!