Stop reload application when orientation changes - android

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?

You need to use a way to persist data between the two screens.
Either use savedInstanceState or ViewModel.

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.

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">

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.

Related

Method calls on android screen rotation

I have an android project in which I had to put a checkbox and a button such that if the checkbox is checked then the button is enabled, otherwise it is not enabled. Firstly I implemented this by setting onClick attribute of the checkbox, but when the checkbox was checked and I rotated the screen the button switched to not enabled although the checkbox is still checked. So I tried to use the setOnCheckedChangeListener method of checkbox and it worked (after screen rotation the button was still enabled). Why does it happen?
When you rotate the screen, the activity is basically recreated. Some state information may be automatically preserved, but you should become more familiar with the overall activity lifecycle to understand the subtle differences.
activity lifecycle
In addition to Jerry's answer, to get around the issue quickly, you can simply make boolean member variables in your activity for each of your check boxes. These should then persist through orientation change.
Jerry is right, make sure you know about the Android Lifecycle.
Add this line of code in your activity inside your manifest file.
android:configChanges="orientation"

Handle orientation change android

I am having a requirement where there are several buttons in a activity, and clicking on one will change the background. The activity refreshes on orientation change. Then i included, android:configChanges="keyboardHidden|screenSize"
which solves the problem, but i have separate screens for portrait and landscape modes, this saves the state and do not pick the layout for landscape when the orientation is changes. Please guide how i can change the orientation but still prevent the activity refresh and thus saving the clicked button and background color.
dont use android:configChanges="keyboardHidden|screenSize"
and use onSaveInstanceState() for return color or resource id of background and then check it in oncreate() for set background
this example help you
How to use onSavedInstanceState example please
used this
android:configChanges="keyboardHidden|orientation|screenSize"

Orientation Change - Update UI

I use data of the accelerometer in my app. If the device´s angle is changed the screen orientation should change.
In order to change the orientation I have 2 layouts, one for portrait and one for landscape.
When the orientation changes a function is called that changes the layout with:
setContentView(R.layout.landscape); or setContentView(R.layout.portrait);
This works fine but I have a problem with several UI elements like buttons or ToggleButtons.
I initialize a onClickListener in the onCreate Method for them and each time the orienation gets changed I initialize a new onClickListener.
Unfortunately I can´t change the state of ToggleButtons anymore.
How can I solve this problem?
you can use /layout-land and /layout-port and the phone will automatically switch them for you on orientation change. see
http://developer.android.com/guide/practices/screens_support.html#DesigningResources
as for the toggle buttons, see onRetainNonConfigurationInstance()
http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29
As described by Bill Gary above, why not just let the system take care of orientation changes for you? Also the standard system widgets should automatically preserve their state when you do this. See here for why and how to manage custom state, if you need to.

Override Orientation Change But NOT Restart The Activity AND Pass State Data

I want to be able to change the layout when a device is re-orientated to landscape or portrait. For speed and resource purposes (plus other issues applicable to my app) I do NOT want my app to be destroyed and restarted. I have several objects which I wish to retain between orientation changes as there is no benefit from destroying and re-creating them! I simply just want to change the position of some buttons and TextViews so that they suit the current orientation. Easy right?
Well no it isn't. To achieve the above I included in the app Manifest the configChange option for orientation change. Then I've implemented the onConfigurationChanged() where I determine and apply the appropriate layout. Simple yes?
But now take the textview I have in my layout. How on earth, using this particular method of responding to orientation changes, do I put the same text in the previous textview to the new textview? No instance data is passed to onConfigurationChanged() method. Also for some of the buttons, they could be disabled or enabled... I need to know this after orienatation change.
If I let Android destroy and restart my activity it's first going to create unnecessary work. All I want is to just move a few buttons and textviews.. NOT restart the whole app. That's just ludicrous!
Can anyone help me achieve what need?
An easy way to maintain configuration-independent data is to make use of onRetainNonConfigurationInstance() and its companion method getLastNonConfigurationInstance(). Just return an object that contains all the data that you want to reuse when your activity is recreated.
In Honeycomb, or if you are using the Android compatibility package, you can just call Fragment.setRetainInstance(true) instead. See the docs.

Android custom component doesn't retain value on orientation change

I have a layout that uses TextView and a custom control I created that combine a TextView and two buttons for an integer up/down control. The TextView retains its value when the device is rotated, I'm calling the save/restore instance state. I'm not capturing any values as the device retains the values. However, this is not the case for my custom component.
Is there something I need to implement in a custom control so when the device orientation changes it retains the values like the native controls do?
Thank you.
Throw android:configChanges="orientation" into your activity tags in your AndroidManifest.xml file.
You can prevent your activity from being destroyed and recreated as Haphazard suggests, and this is probably the best solution.
If you don't want to do that, then you need override the following three methods:
isSaveEnabled() (override to return true)
onSaveInstanceState()
onRestoreInstanceState(Parcelable)
Consult the documentation (such as it is) for how to do this. There's also an example of how it's done here. The view will need a non-default ID to have it's onSaveInstanceState() called or else you'll also need to override onSaveInstanceState(Bundle) in your activity.

Categories

Resources