Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android App orientation change restarts activity

Tags:

android

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 given below:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.sample.appname.MainActivity"
        android:label="@string/app_name"
        android:configChanges="orientation|keyboardHidden"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

In the above code snippet android:configChanges="orientation|keyboardHidden" is supposed to do the work for me but the activity is still getting restarted. Please let me know how to correct it.


1 Answers

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

like image 62
l7r7 Avatar answered Oct 25 '25 17:10

l7r7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!