Reputation: 39
I have a problem when I rotate my device, my activity is re-executed and the values change.For example, suppose that you have an activity with a button, when this button is clicked, it will show the increased value in a text view (a counter). And when you put your device in landscape mode, the activity will run again, and you will see (again) the value increased. My question is, is there a way to not run the activity again when you rotate the device ?
Upvotes: 0
Views: 351
Reputation: 381
add this to the manifest
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
If configChanges contains uiMode, remove it. If you don't want to uninstall uiMode, you can update these libraries.
androidx.appcompat:appcompat:1.2.0-rc01
androidx.core:core:1.3.2
Upvotes: 0
Reputation: 3050
Please read this. You will see that there are two solutions for handling configuration changes:
You can declare that you are handling the change on your own by updating activity in the manifest. This way your activity will not be restarted and onConfigurationChanged()
will be called:
<activity ...
android:configChanges="orientation|screenSize"
...>
You can pass your data objects to the new activity via onSaveInstanceState()
function. Passing large objects can slow down the app. It is better you separate data from the view for example by using ViewModels, which are retained in these kind of changes.
First way may seem easy, but it is not recommended as you can see in the link provided at the beginning:
Caution: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
Upvotes: 2
Reputation: 3273
In the manifest file add android:configChanges = "orientation|screenSize"
to the activity tag.
For ex:
<activity
android:name=".dummyActivity"
android:configChanges = "orientation|screenSize"/>
If you want to handle orientation changes manually you can do so by overriding the onConfigurationChanged()
method in Activity.
For more info refer Handling Configuration Changes
Upvotes: 0
Reputation: 18416
Add android:configChanges in your activity
<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>
Upvotes: 0