android implements an example of sharing where the page information is not reset when switching between horizontal and vertical screens

  • 2020-05-27 07:06:11
  • OfStack

When the screen rotates and switches, the Android mechanism is:
Destroy the Activity of the current screen and restart a new Activity that ADAPTS to screen changes.
So, how do we not reset the information when the screen is switched?

Solution realization:
1. Add the activity element of AnroidMainifest.xml:


android:configChanges="orientation|keyboardHidden"

or

android:configChanges="orientation|keyboard|keyboardHidden"

Means to stop executing the onCreate() method when changing the screen orientation, popping up the software disk, and hiding the soft keyboard,
Instead, directly execute onConfigurationChanged().
If this code is not declared, Activity will execute the onCreate() method once according to the lifetime of Activity,
The onCreate() method usually does some initialization before displaying.

So if you change the orientation of the screen and you do onCreate(), you're going to have repetitive initialization,
Reducing the efficiency of the program is inevitable, and it is more likely that data will be lost due to repeated initialization.
This is something to avoid!

2. Permission statement:


<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>

API says this permission allows us to change configuration information, but we don't use it in programs that change screen orientation. Is that a conflict?
So the way to think about it is, when we declare this permission,
The system allows us to capture and modify certain configuration information by overriding the onConfigurationChanged method in activity.

3. Rewrite the onConfigurationChanged method in Activity in the Java source code file:


import android.content.res.Configuration;
//  Frame callback function  onConfigurationChanged  from  android.content.res.Configuration  The package. 
//  parameter  newConfig -  The installation of new equipment. 
//  When there is a change in device configuration information (such as a change in screen orientation, physical keyboard pushing, etc.), 
//  And if there is  Activity  Running, the system will call this function. 
//  Note: onConfigurationChanged  Will only respond to the application in  AnroidMainifest.xml  In the 
//  through  android:configChanges=" Configuration type "  Changes to the specified configuration type; 
//  For other configuration changes, the system will destroy the current screen first  Activity  . 
//  And I'm gonna turn it back on 1 A new adaptation to screen changes  Activity  Instance. 
public void
onConfigurationChanged( Configuration newConfig )
{
    // 1 Make sure you call the superclass's function of the same name first, and let the framework default function handle it first 
    //  The following 1 Must not be omitted, otherwise it will cause: android.app.SuperNotCalledException  The exception. 
    super.onConfigurationChanged( newConfig );

    //  Detect screen orientation: vertical or horizontal 
    if ( this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE )
    {
        //  It's currently landscape,   Add additional processing code here 
    }
    else if ( this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT )
    {
        // It's currently portrait,   Add additional processing code here 
    }

    // Detect the status of the physical keyboard: push it out or close it     
    if ( newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO )
    {
        //  The physical keyboard is in the out state, where additional processing code is added 
    }
    else if ( newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES )
    {
        //  The physical keyboard is in the closed state, where additional processing code is added 
    }
}

1. Create a new Activity and print out each life cycle:
Step 1:
Run Activity and get the following information:


onCreate
onStart
onResume

Step 2:
Press crtl + f12 to switch to landscape:


onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume

Step 3:
Then press crtl + f12 to switch to portrait, and the same message is printed twice:


onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume
onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume

Step 4:
Modify the Activity element in the AndroidManifest.xml file,
Add android: configChanges = "orientation",
When switching to landscape by pressing crtl + f12 :(same as when no changes were made above)


onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume

Step 5:
Then press crtl + f12 to switch to portrait,
It turns out that you won't print the same information again,
But one more line of onConfigChanged was printed:


onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume
onConfigurationChanged

Step 6:
Modify the Activity element in the AndroidManifest.xml file,
the


android:configChanges="orientation"

to

android:configChanges="orientation|keyboardHidden"

When pressing crtl + f12 to switch to landscape,
Just print onConfigChanged:


onConfigurationChanged

Step 7:
When switching to portrait by pressing crtl + f12:


onConfigurationChanged
onConfigurationChanged

2. Conclusion:
1. When the android:configChanges element of Activity in AndroidManifest.xml file is not set,
Each life cycle will be called again when cutting the screen. It will be executed once when cutting the landscape screen and twice when cutting the portrait screen.

2. Set the Activity element in the AndroidManifest.xml file
android: configChanges = "orientation",
Screen cut or call each life cycle again, the horizontal, portrait cut will only be executed once;

3. Set the Activity element in the AndroidManifest.xml file
android: configChanges = "orientation | keyboardHidden",
The screen cut does not re-invoke the lifecycle, only the onConfigurationChanged method is executed!

3. One additional point:
1. The life cycle of Activity will not change when the current Activity event pops up Toast and AlertDialog!

2. Press the home key when Activity is running (as if it were completely overwritten) :


onSaveInstanceState --> onPause --> onStop 
onRestart --> onStart --> onResume

3.Activity is not completely covered and just loses focus:


onPause --> onResume


Related articles: