①Call onSaveInstanceState(Bundle outState) of the current Activity to save some data in the application. You can override this method
②Then call onDestroy() to destroy the current Activity
③Recall the onCreate() or onRestoreInstanceState() method to recreate an Activity (that is, the program will re-execute the code in onCreate)
In addition, one thing that needs to be explained is how to use onSaveInstanceState(Bundle outState): This method will be called when the screen is rotated. The parameter Bundle here is to encapsulate what we want before the screen is rotated. of the saved data, then call onDestroy() to destroy the current Activity, and finally call onCreate(Bundle savedInstanceState) again. At this time, outState will be assigned to savedInstanceState. In this case, when we re-create the Activity in onCreate, we can use the previously saved This is very important because during the entire running process of an application, regardless of whether it is in horizontal or vertical screen, the data changes must be consistent. The user cannot be asked to start from scratch just because the screen changes. Start operating again
In addition, since the onSaveInstanceState(Bundle outState) method will definitely be triggered when the screen rotates, then we can completely use this method as a monitor for screen rotation🎜
🎜Here’s an example: 🎜
🎜*Note that it is not a simple matter to judge the horizontal screen or the vertical screen, because Android stipulates that only when the screen is completely vertical or horizontal can ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE and ActivityInfo.SCREEN_ORIENTATION_PORTRAIT be returned, which is almost impossible in practice. Such a situation exists 🎜
🎜Here’s how: 🎜
🎜First: Permission Statement: 🎜
🎜<uses-permission Android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>🎜
🎜Second: Declare the event type to be captured by the activity, 🎜
🎜The Android:configChanges attribute must be declared here. This attribute specifies the event types we can capture in the program. Multiple event types are separated by |. 🎜
🎜If there is no orientation here, then we cannot capture the screen change event in the program. 🎜
🎜Third: Rewrite the onConfigurationChanged method in Activity🎜
You need to know the following points
1.Settings
<activity>The android:screenOrientation attribute of the node can complete this task. The sample code is as follows:
2. Force the screen rotation effect to be turned on
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
3.监听屏幕旋转
这里是方法:
第一:权限声明:
3. Monitor screen rotation<uses-permission Android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
🎜Here’s how: 🎜 🎜First: Permission Statement: 🎜 🎜
<uses-permission Android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
🎜 🎜Second: Declare the event type to be captured by the activity, 🎜 🎜The Android:configChanges attribute must be declared here. This attribute specifies the event types we can capture in the program. Multiple event types are separated by |. 🎜 🎜If there is no orientation here, then we cannot capture the screen change event in the program. 🎜 🎜Third: Rewrite the onConfigurationChanged method in Activity🎜 🎜If you really don’t know how, you can contact me on the site. I can help you take a look and we can make progress together🎜Although you didn’t answer my question directly, I used what you said today. Thank you very much.