Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change screen orientation in Android without reloading the activity

I want to change the orientation programmatically while running my Android App, with these lines of code:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

They work so far, but the main problem is that the whole activity is reloaded when the screen orientation changes, and I don't want that. Is it possible? Thanks.

EDIT: OK, after I while I found out what I was missing. I had to include also "screenSize" in the configChanges property, so having

android:configChanges="orientation|screenSize"

solved the whole thing.

like image 682
luisfer Avatar asked Feb 14 '13 11:02

luisfer


People also ask

How do I stop Android from restarting activity when changing orientations?

If you want the activity to not restart during screen orientation change, you can use the below AndroidManifest. xml. Please note the activity android:configChanges=”orientation|screenSize” attribute. This attribute makes the activity not restart when change screen orientation.

How do you prevent data from reloading and resetting when the screen is rotated?

Just add android:configChanges="orientation|screenSize" in activity tab of manifest file. So, Activity won't restart when orientation change.

How do I change the orientation of my Android phone manually?

1 Swipe down the screen to access your Quick Settings and tap on Auto Rotate, Portrait or Landscape to change your screen rotation settings. 2 By selecting Auto Rotate,you will easily be able to switch between Portrait and Landscape mode. 3 If you choose Portrait this will lock the screen from rotating to landscape.


4 Answers

See the edit by @luisfer. For targeting Android 3.2 and above, you need BOTH

android:configChanges="orientation|screenSize"

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

like image 85
larham1 Avatar answered Oct 07 '22 10:10

larham1


You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter

like image 29
Martin Avatar answered Oct 07 '22 10:10

Martin


In AndroidManifest file add android:configChanges="orientation" for the activity you want to handle this orientation

In activity use onConfigurationChange overrided method. Do task you want to handle in orientation change.

like image 1
Raj Shah Avatar answered Oct 07 '22 09:10

Raj Shah


Ansewered here:

Android, how to not destroy the activity when I rotate the device?

Add:

android:configChanges="orientation"

To your androidmanifest.

see:

http://developer.android.com/guide/topics/manifest/activity-element.html#config

like image 1
inigoD Avatar answered Oct 07 '22 09:10

inigoD