Handle Configuration Change in Android

Tram Ho

Some device configurations may change while the application is running (for example, screen orientation). When this change happens, Android will restart Activity (onDestroy () will be called, then onCreate ()) to adjust to the new configuration.

To enhance the user experience, you need to deal with it before and after the Configuration Change. Example: On the following screen:

When you click on +1 or -1, the center value will be increased or decreased by 1 unit. If we do not handle this value will be returned to the original value:

1. Fixed activity in a certain direction.

If the application is not needed (or more specifically, customers, teachers, teachers … not required: v), you can fix the activity in a certain direction PORTRAIT (vertical) or LANDSCAPE (horizontal) In this way, you only need to define the android: screenOrientation attribute for the activity in the Manifest file. ex:

Thus, you have the activity installed only for viewing in portrait screen mode. The orientation of the application screen will not be rotated even if the user turns on the screen rotation mode on the device

2. Handling through the lifecycle of Activity

When there is a Configuration Change, before the activity calls onDestroy (), the onSaveInstanceState () function will be called, this function has a parameter of 1 Bundle, you will save the data of the current activity into this Bundle. And after onCreate () is called, the activity will call onRestoreInstanceState (), which also has a Bundle parameter containing the data that you saved in the onSaveInstanceState () function.

For example: activity_main.xml:

MainActivity.java

And this is the result:

3. Use the ViewModel.

The ViewModel is a component designed to store and manage data in a separate lifecycle. It allows data to be preserved even when the Configuration Change occurs. To gain a deeper understanding of ViewModel, please visit the following link: https://developer.android.com/topic/libraries/architecture/viewmodel . Example of using ViewModel: activity_main.xml

MainViewModel.java

MainActivity.java

The results are similar when handled by Activity’s life cycle.

4. Handling the Configuration Change yourself

If the Activity does not need to update the resource (the vertical and horizontal screen UI has different structure) or you do not want the Activity reset to occur. To be able to handle Configuration Change by yourself, declare the android: configChanges property for Activity in Manifest with a value that represents the configuration function you want to handle (commonly used as “orientation” to prevent starting restart the activity when the direction changes, other parameters you can see here )

When declared to handle Configuration Change by itself, whenever a change is made, Activity will call onConfiguationChanged () and you can update some of the changed resources in this function (for example, language).

Note: Since API 13, the screen size also changes when Configuration Change is available, so you need to add the screenSize value to the android: configChanges property.

For example: In the application you are needing to load data from the server, If in the usual way, Activity will initialize and recall your data load function when Configuration Change occurs. Doesn’t this sound good? In this case, you should declare yourself to handle and Activity do not need to be reinitialized and the data will not be reloaded, and you will handle some updates in the onConfigurationChanged () function (for example, switch from display list of data from 1 column to 2 columns and vice versa If you do not need to update anything, you do not need to override the function onConfigurationChange ().

At that time, MainActivity will not be initialized when the screen orientation changes, so in MainActivity.java you don’t need to do anything.

Share the news now

Source : Viblo