Android Activity Life Cycle switch the screen anyway Detailed

Activity on when life cycle and switch the screen anyway, during the execution of the life cycle, there are many online articles. But it is written in very vague and imperfect. Generally, we are going to switch the screen orientation does not want Activity recreated, then you need to set some properties, or use the code set. Through the above aspects of the text analysis Activity in the horizontal and vertical screen switch, life-cycle approach the implementation process.

  • 1.Activity life cycle

  • 2.configChanges resolve property

  • 3.orientation property

  • 4.keyboardHidden property

  • 5.screenSize property

  • 6. Summary

  • activity life cycle approach
    by default, activity from creation to destruction will perform the following life-cycle approach

onCreate -->onStart–>onResumeo -->nPause -->onStop -->onDestroy

  • Detailed configChanges property

1.orientation screen rotation between longitudinal and lateral
2.keyboardHidden keyboard display or hide
3.screenSize screen size change
4.fontScale user changes the preferred font size
5.locale different language selected by the user is set
6.keyboard keyboard type changes, such as mobile phones switched from keyboard to keyboard 12
changes 7.touchscreen keyboard or navigation or navigation, in general such incidents do not occur
frequently used include: orientation keyboardHidden screenSize, set three interface does not go Activity life cycle, onConfigurationChanged only callback method.

  • Detailed property screenOrientation

1.unspecified default values, the system automatically determines the state of the switching
2.landscape landscape
3. portrait portrait
orientation value currently set user 4.user
orientation value Activity 5. behind the next to be displayed
6. sensor using a sensor of the sensor direction
7. nosensor without using a sensor substantially identical to the usual portrait and landscape only unspecified, the default interface is representative of horizontal screen or vertical screen, can also change the code again.

1.AndroidManifest property is not set configChanges

  • Portrait start: onCreate -> onStart-> onResume
  • 切换横屏: onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate–>onStart --> onRestoreInstanceState–>onResume -->onPause -->onStop -->onDestroy
  • Horizontal screen start: onCreate -> onStart-> onResume
  • 切换竖屏: onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate–>onStart --> onRestoreInstanceState–>onResume -->onPause -->onStop -->onDestroy

2.AndroidManifest set configChanges android: configChanges = "orientation"

  • Portrait start: onCreate -> onStart-> onResume
  • 切换横屏: onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate–>onStart --> onRestoreInstanceState–>onResume -->onPause -->onStop -->onDestroy
  • Horizontal screen start: onCreate -> onStart-> onResume
  • 切换竖屏: onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate–>onStart --> onRestoreInstanceState–> onResume -->onPause -->onStop -->onDestroy

3.AndroidManifest设置了configChanges android:configChanges=“orientation|keyboardHidden|screenSize”

  • Vertical (horizontal) screen start: onCreate -> onStart-> onResume
  • Switching horizontal (vertical) Screen: onConfigurationChanged (Android 6.0 Android 7.0 Android 8.0)

4.AndroidManifest set configChanges android: configChanges = "orientation | screenSize"

  • Vertical (horizontal) screen start: onCreate -> onStart-> onResume
  • Switching horizontal (vertical) Screen: onConfigurationChanged (Android 6.0 Android 7.0 Android 8.0)
  • Note : The code dynamically set the horizontal and vertical screen state (when the screen changes when onConfigurationChanged callback)

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  • Summary
    1. configChanges property is not set, the current interface call onSaveInstanceState go over the process, and then restart the call onRestoreInstanceState go again complete the process, and ultimately destory.
    2. After setting up configChanges property orientation, Android6.0 with no set configChanges same situation, we have completed two complete life cycle, called onSaveInstanceState and onRestoreInstanceState methods; Android 7.0 will be the first onConfigurationChanged callback method, with the rest of the process Android 6.0 is consistent; Android 8.0 system is simple,
    just a onConfigurationChanged callback method, and did not take a life-cycle approach Activity.
    3. Set the android: configChanges = "orientation | keyboardHidden | screenSize" it will not call other life-cycle approach Activity will only call onConfigurationChanged method.
    4. Without keyboardHidden with 3 is the same, orientation representatives had to switch screens screenSize behalf screen size is changed,
    set both these methods will not callback Activity life cycle, only the callback onConfigurationChanged.
Published 11 original articles · won praise 30 · views 90000 +

Guess you like

Origin blog.csdn.net/jaynm/article/details/104560439
Recommended