Life Cycle Method for android Horizontal and Vertical Screen Switching

Keywords: Attribute Android

1. Why do we need to re-invoke the lifecycle method when switching the horizontal and vertical screen? If not, it will save more resources.
Whether you rotate to the horizontal screen or to the vertical screen, when the rotation occurs, Android will look for more appropriate resources (layout files) to match the device configuration. Then the current activity instance is destroyed and an activity instance is rebuilt according to the new configuration. So the corresponding life cycle function will be called, once: destroy first, then rebuild:
2. An activity, print log, code is as follows, here only in each method print log

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("MainActivity", "onCreate: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("MainActivity", "onStart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("MainActivity", "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("MainActivity", "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("MainActivity", "onStop: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("MainActivity", "onDestroy: ");
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        Log.e("MainActivity", "onSaveInstanceState: ");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e("MainActivity", "onRestoreInstanceState: ");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.e("MainActivity", "onConfigurationChanged: ");
    }
}

(1) Running on virtual machine, switching horizontal and vertical screen, version 5.1
Vertical screen cutting horizontal screen:

E/MainActivity: onPause: 
E/MainActivity: onStop: 
E/MainActivity: onDestroy: 
E/MainActivity: onCreate: 
E/MainActivity: onStart: 
E/MainActivity: onRestoreInstanceState: 
E/MainActivity: onResume: 

Horizontal screen cutting vertical screen:

E/MainActivity: onPause: 
E/MainActivity: onStop: 
E/MainActivity: onDestroy: 
E/MainActivity: onCreate: 
E/MainActivity: onStart: 
E/MainActivity: onRestoreInstanceState: 
E/MainActivity: onResume: 

(2), version 4.4, real machine running (Hua4c)
Vertical screen cutting horizontal screen

E/MainActivity: onPause: 
E/MainActivity: onStop: 
E/MainActivity: onDestroy: 
E/MainActivity: onCreate: 
E/MainActivity: onStart: 
E/MainActivity: onRestoreInstanceState: 
E/MainActivity: onResume: 

Horizontal Screen Cutting Vertical Screen

E/MainActivity: onPause: 
E/MainActivity: onStop: 
E/MainActivity: onDestroy: 
E/MainActivity: onCreate: 
E/MainActivity: onStart: 
E/MainActivity: onRestoreInstanceState: 
E/MainActivity: onResume: 

(3) Real machine running, android:configChanges= "orientation" in activity
Vertical Screen Switching Horizontal Screen

E/MainActivity: onConfigurationChanged: 
E/MainActivity: onPause: 
E/MainActivity: onStop: 
E/MainActivity: onDestroy: 
E/MainActivity: onCreate: 
E/MainActivity: onStart: 
E/MainActivity: onRestoreInstanceState: 
E/MainActivity: onResume: 

The onConfigurationChanged method is called more than when this attribute is not added.

Horizontal Screen Switching Vertical Screen

E/MainActivity: onConfigurationChanged: 
E/MainActivity: onPause: 
E/MainActivity: onStop: 
E/MainActivity: onDestroy: 
E/MainActivity: onCreate: 
E/MainActivity: onStart: 
E/MainActivity: onRestoreInstanceState: 
E/MainActivity: onResume:

It's the same as the vertical screen switching the horizontal screen.
(4) Real machine running, add attribute: android:configChanges="orientation | keyboard Hidden"
Vertical screen switching horizontal screen and horizontal screen switching vertical screen:

E/MainActivity: onConfigurationChanged: 
E/MainActivity: onPause: 
E/MainActivity: onSaveInstanceState: 
E/MainActivity: onStop: 
E/MainActivity: onDestroy: 
E/MainActivity: onCreate: 
E/MainActivity: onStart: 
E/MainActivity: onRestoreInstanceState: 
E/MainActivity: onResume: 

(5) Real machine running, add attributes:
Vertical screen switching horizontal screen, horizontal screen switching vertical screen

E/MainActivity: onConfigurationChanged: 

Only this method will be invoked.

(6) Running on a virtual machine is different from the real machine. Specific estimates are made of the difference between the android version and the real machine. In the actual development, the real machine is used, so it is no longer studied here.

(7) Press the home key and then start walking.

E/MainActivity: onResume: 
E/MainActivity: onPause: 
E/MainActivity: onStop: 
E/MainActivity: onRestart:
E/MainActivity: onStart: 
E/MainActivity: onResume: 

(8) The power button turns off the screen.

E/MainActivity: onPause: 
E/MainActivity: onStop: 
E/MainActivity: onStart: 
E/MainActivity: onResume: 

Posted by webosb on Thu, 21 Mar 2019 15:33:53 -0700