Home > Net >  Stop reload application when orientation changes
Stop reload application when orientation changes

Time:03-05

I need my app to not reload when the orientation changes. I have a regular portrait and a seperate landscape layout that both have the same stuff on the screen. But when I check a checkbox and change layout, it's no longer checked. Or when a checkbox has the number 5 in it and then change layout it goes back to it's default number 1. How can I make it stop reloading?

CodePudding user response:

add this following permission for your activity in Manifest file android:configChanges="orientation|screenSize

Something like this

<activity
        android:name=".YourActvity"
        android:configChanges="orientation|screenSize">

CodePudding user response:

You need to use a way to persist data between the two screens.

Either use savedInstanceState or ViewModel.

CodePudding user response:

First tip from Philipp Lackner explains how to use the savedStateHandle https://www.youtube.com/watch?v=0d5ax97Mb30

The state when you change orientation, for example, gets restored, so you need to handle via viewmodel the state persistence.

CodePudding user response:

The checked state of a CheckBox and text in an EditText will be automatically preserved if you make sure they have the same id in both layouts. However, it only automatically preserves the things that can be changed through the UI, so if you programmatically changed the text of a CheckBox, that is not automatically preserved. Typically, that sort of thing is handled with a ViewModel and LiveData/Flow.

This behavior is controlled by the saveEnabled property of a view, which is true by default.

  • Related