Android App orientation change restarts activity -


i have activity , getting restarted whenever orientation changes. have written code prevent activity restart upon change in orientation in manifest file 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 above code snippet android:configchanges="orientation|keyboardhidden" supposed work me activity still getting restarted. please let me know how correct it.

actually shouldn't prevent activity restarted. neccessary recreate activity after rotation change several reasons. 1 of layout has inflated deal changed screen size , things (it's easy imagine layout totally different in portrait in landscape mode). however, there way can tell system deal screen changes yourself. therefore change line in manifest

android:configchanges="orientation|keyboardhidden" 

to

android:configchanges="orientation|screensize" 

the activity won't recreated then. you'll callback via onconfigurationchanged() method can when orientation has changed. if don't want when configuration has changed, don't override onconfigurationmethod() in activity. read this section in android developers api guide more information.

i got this answer. there 2 more approaches in answer think 1 described above best in case.

edit: maybe have add keyboard|keyboardhidden android:configchanges attribute well, stated in this answer


edit #2: if want retrieve current orientation of device can call

activity.getresources().getconfiguration().orientation 

which return constants orientation_portrait or orientation_landscape.

if you're interested in exact rotation angle, use

int rotation =  getwindowmanager().getdefaultdisplay().getrotation(); 

and implement differentiation switch case described here.

and third way determine device's rotation getrequestedorientation() return constant defined in the documentation


Comments