The way android handles configuration changes
This article mainly introduces the relevant knowledge of android's method of dealing with configuration changes, the content is detailed and easy to understand, the operation is simple and fast, and it has certain reference value. I believe everyone will gain something after reading this android method of dealing with configuration changes. Let's take a look.
1. Configuration change
Some device configurations may change at run time (such as screen orientation, keyboard availability, and when the user enables multi-window mode). When this change occurs, Android restarts the running Activity (calling onDestroy () and then onCreate ())
If this problem is not addressed, it will result in the loss of data that is already bundled to the control. Here are two solutions to this problem (the first is to save the data, and the second is to configure the data not to be lost).
two。 Handle configuration changes: retain objects during configuration changes
Can be implemented using viewmodel
3. Handle configuration changes: handle configuration changes on your own
3.1 prevent activity restart through configuration
You can declare that Activity handles configuration changes on its own by configuring the configChanges property in the activity standard, thus preventing the system from restarting Activity, whose value represents the configuration to be processed
To prevent the screen from turning, you need to configure "orientation" and "screenSize"
The "keyboardHidden" value prevents rebooting when keyboard availability changes
3.2 configuration change snooping
If you need to do something after the configuration change, you can change it from writing onConfigurationChanged to listening as follows: listen to the screen direction code.
@ Override public void onConfigurationChanged (Configuration newConfig) {super.onConfigurationChanged (newConfig); if (newConfig.orientation = = Configuration.ORIENTATION_LANDSCAPE) {Toast.makeText (this, "landscape", Toast.LENGTH_SHORT). Show ();} else if (newConfig.orientation = = Configuration.ORIENTATION_PORTRAIT) {Toast.makeText (this, "portrait", Toast.LENGTH_SHORT). Show () }} this is the end of the article on "how android handles configuration changes". Thank you for reading! I believe you all have a certain understanding of the knowledge of "android's method of handling configuration changes". If you want to learn more, you are welcome to follow the industry information channel.