Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Stop Recreating the activity on orientation change

Tags:

I have a listview with two buttons in my main.xml layout. On click of one button i'am creating a textview dynamically and adding it at the bottom of the screen to confirm the user interaction. When the user clicks 2nd button (Confirm button), i need to add that text to listview. To support landscape mode, i have the same layout file in layout-land folder. When i click on 1st button it is creating a textview with some text and adding at bottom of the screen. Now if a change the device orientation then it is loading the landscape main.xml and activity is recreating again. So my textview is getting collapsed. How can i prevent that the recreation of activity on orientation change. (But it should pick up the other layout file).

like image 958
sachi Avatar asked May 10 '12 09:05

sachi


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.

What happens to an activity when the screen orientation changes?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.


1 Answers

Just edit the Activity Tag in androidmanifest.xml.

<activity
            android:configChanges="keyboardHidden|orientation"
            android:name=".testActivity"
            android:label="@string/app_name"></activity>
like image 98
Dhruvil Patel Avatar answered Oct 12 '22 02:10

Dhruvil Patel