Skip to content Skip to sidebar Skip to footer

Android App Orientation Change Restarts Activity

I have an activity and it is getting restarted whenever orientation changes. I have written code to prevent activity restart upon change in orientation in the manifest file as give

Solution 1:

Actually you shouldn't prevent the Activity to be restarted. It is neccessary to recreate the Activity after a rotation change for several reasons. One of it is that the layout has to be inflated to deal with the changed screen size and things like that (it's easy to imagine that the layout is totally different in portrait than it is in landscape mode). However, there is a way you can tell the system that you deal with the screen changes by yourself. Therefore change the line in your manifest

android:configChanges="orientation|keyboardHidden"

to

android:configChanges="orientation|screenSize"

The activity won't be recreated then. You'll get a callback via the onConfigurationChanged() method so you can do something when the orientation has changed. If you don't want to do anything when the configuration has changed, just don't override the onConfigurationMethod() in your Activity. Read this section in the Android Developers API Guide for more information.

I got this from this answer. There are two more approaches in the answer but I think the one I described above is the best in your case.

EDIT: Maybe you have to add keyboard|keyboardHidden to the android:configChanges attribute as well, as stated in this answer


EDIT #2: If you want to retrieve the current orientation of the device you can call

Activity.getResources().getConfiguration().orientation

which will return the constants ORIENTATION_PORTRAIT or ORIENTATION_LANDSCAPE.

If you're interested in the exact rotation angle, use

int rotation =  getWindowManager().getDefaultDisplay().getRotation();

and implement a differentiation with something like a switch case described here.

And a third way to determine the device's rotation is getRequestedOrientation() which will return a constant defined in the documentation

Solution 2:

android:configChanges="orientation|screenSize" means whenever screen orientation will be changed a following method in Activity class will be called.

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { body }
    }

This is nature of andorid that whenever screen orientation is changed whole lifecycle of activity runs again.

To save and restore some data. you can use following methods.

@OverridepublicvoidonSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);

    outState.putString("key","value");
}

@OverrideprotectedvoidonRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    String s = savedInstanceState.getString("key");

}

Solution 3:

Try changing

android:configChanges="orientation|keyboardHidden"

Into

android:configChanges="orientation|screenSize"

this should solve your problem, but it is not a efficient way, for a better option refer http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Post a Comment for "Android App Orientation Change Restarts Activity"