The life cycle of Android activity and the state change of the activity when the vertical screen is switched across the screen

Source: Internet
Author: User

life cycle
The Android system adds hooks to the activity life cycle, and we can do something in the hooks that these systems reserve.
Examples of 7 commonly used hooks:
protected void OnCreate (Bundle savedinstancestate)
protected void OnStart ()
protected void Onresume ()
protected void OnPause ()
protected void OnStop ()
protected void Onrestart ()
protected void OnDestroy ()

Brief description:
OnCreate (Bundle savedinstancestate): Called when an activity is created. In this method, the information needed to create the Activity can also be presented in bundles.
OnStart (): Called when activity becomes visible to the user on the screen, that is, when the focus is obtained.
Onresume (): Called when activity starts interacting with the user (whether it is starting or restarting an activity, the method is always called).
OnPause (): Called when activity is paused or retracted by CPU and other resources, which is used to save the active state of the.
OnStop (): Called when activity is stopped and turned into an invisible phase and subsequent life-cycle events, that is, when the focus is lost.
Onrestart (): Called when activity is restarted. The activity is still in the stack, not the start of a new activity.
OnDestroy (): Called when activity is completely removed from system memory, the method is called probably because someone calls the finish () method directly or the system decides to stop the activity to free up resources.

Toggle Screen

1 Switch to horizontal screen
Onsaveinstancestate
OnPause
OnStop
OnDestroy
OnCreate
OnStart
Onrestoreinstancestate
Onresume

2 Switch to vertical screen, destroy twice
Onsaveinstancestate
OnPause
OnStop
OnDestroy

OnCreate
OnStart
Onrestoreinstancestate
Onresume
Onsaveinstancestate
OnPause
OnStop
OnDestroy
OnCreate
OnStart
Onrestoreinstancestate
Onresume

3 Modify the Androidmanifest.xml, add the activity android:configchanges= "orientation", cut the horizontal screen, only destroy once.

Onsaveinstancestate
OnPause
OnStop
OnDestroy
OnCreate
OnStart
Onrestoreinstancestate
Onresume

4 and then cut back to the vertical screen, and found that the same information will not be printed, but more than one line printed onconfigchanged

Onsaveinstancestate
OnPause
OnStop
OnDestroy
OnCreate
OnStart
Onrestoreinstancestate
Onresume
Onconfigurationchanged

5 Change android:configchanges= "orientation" to android:configchanges= "Orientation|keyboardhidden", cut horizontal screen, Just print onconfigchanged

Onconfigurationchanged

6 cutting back to the vertical screen

Onconfigurationchanged
Onconfigurationchanged

Summarize:

1, do not set the activity of the android:configchanges, the screen will recall the various life cycle, cut across the screen will be executed once, cut the vertical screen will be executed twice

2, set the activity android:configchanges= "orientation", the screen will recall the various life cycle, cut horizontal, vertical screen will only be executed once

3. When setting the activity's android:configchanges= "Orientation|keyboardhidden", the screen will not recall each life cycle, only the Onconfigurationchanged method will be executed. (You must set SDK version greater than or equal to 13.0 to execute this method)

The life cycle of Android activity and the state change of the activity when the vertical screen is switched across the screen

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.