Qianfeng-android-day05-activity life cycle, actitity start mode Basic Learning:

Source: Internet
Author: User
Tags home screen

First, Activity life cycle
(a), what is the life cycle of people?
Fetal
Baby
Children, adolescents, youth, middle-aged
A sudden upheaval
Elderly
Death
(ii), Activity life cycle:
1. There are seven different states in the activity life.
OnCreate ()
OnStart ()
Onresume ()
OnPause ()
OnStop ()
Onrestart ()
OnDestroy ()

1. Open an app and click the Back button to exit:
OnCreate ()--->onstart ()--->onresume ()---Press the back key--->onpause ()--->onstop ()--->ondestroy ()


2. Open an app, then click the Home button to exit, and then reopen the app:
OnCreate ()--->onstart ()--->onresume ()---Click the home button--->onpause ()---onStop ()---Reopen the app--->onrestart ()--- >onstart ()---onresume ()


3. Open an activity and click on the Activity button to jump to secondactivity:
M-oncreate ()--->m-onstart ()--->m-onresume ()---Click the jump button---m-onpause ()-->s-oncreate ()--->s-onstart ()-- ->s-resume ()--->m-onstop ()


4. Open an activity, click on the Activity button to jump to Secondactivity, then click the Back button to exit Secondactivity:
M-oncreate ()--->m-onstart ()--->m-onresume ()---Click the jump button---m-onpause ()-->s-oncreate ()--->s-onstart ()-- ->s-resume ()--->m-onstop ()--
--Click the Back button to exit Secondactivity--s-onpause ()--->m-onrestart ()--->m-onstart ()--->m-onresume ()--->s-onstop ()--->s-ondestroy ()


5. Open an activity, click on the Activity button to jump to Thirdactivity (thirdactivity theme is dialog mode), then click the back key:
M-oncreate ()-->m-onstart ()-->m-onresume ()--click the Jump button--m-onpause ()-->t-oncreate ()-->t-onstart ()-- T-onresume ()--
--Click Back key-->t-onpause ()-->m-onresume ()-->t-onstop ()-->t-ondestroy ()

6. Open an activity and switch from vertical to horizontal screen
OnCreate ()->onstart ()->onresume ()---switch to horizontal screen--->onpause ()->onstop ()->ondestroy ()->oncreate ()- >onstart ()-onresume ()

7. Open an activity and switch from the vertical screen to a horizontal screen (onsaveinstancestate,onrestoreinstancestate):
OnCreate ()->onstart ()-onresume ()--Toggle Horizontal Screen-->onpause ()->onsaveinstancestate ()->onstop ()->ondestroy ( )->oncreate ()->onstart ()->onrestoreinstancestate ()->onresume ()

8. Open an activity and switch from portrait to Landscape (Configure android:configchanges= "Orientation|screensize")
OnCreate ()->onstart ()->onresume ()--Toggle Horizontal screen-->onconfigurationchanged ()
"Remarks:"
A, seven life cycle combinations:
OnCreate, OnStart, Onresume: Launching the application
OnPause, OnStop: Losing focus
Onrestart, OnStart, Onresume: Regain focus
OnPause, OnStop, OnDestroy: Exiting the application
B, seven life cycles are divided by stages:
OnCreate ()---ondestroy () full life cycle the entire lifetime
OnStart ()---onStop () visible life cycle the visible lifetime:
Onresume ()---onPause () Forefront life cycle (focus life cycle) the foreground lifetime:


(iii), the role of life cycle:
① when a user calls or switches to another program does not crash
② valuable system resources are not destroyed when the user runs the program in the background
③ the user's process is not lost when the user leaves and returns to your app
④ does not crash or lose the user's process when the screen of the phone is switched vertically
(iv), instance code operation:
Imitate the following operations, observe the output log, look for the life cycle method in turn callback law:
Open main interface, back key exit
Open the main interface and go to the second page
Open the main interface, simulate the phone to enter and hang the machine, display the main interface again
Open main interface, home key exit
Open the main interface, the home button to quit, start the app again
Open the main screen and click the button in the interface to bundle the exit events and exit
Open the main screen and toggle the vertical screen.
Second, activity startup mode
Task and fallback stack:
(i) Tasks task:
1. Concept:
A task is a collection of activities that interact with the user while performing a job.
These activity is arranged sequentially in a stack (fallback stack) in the order in which they are opened.

2. Main Screen page:
The home screen of the device is the startup location for most tasks, and when the user touches an app launcher icon (or app shortcut icon),
The application's tasks are displayed in the foreground. If the task for the associated application does not exist, a new task is created,
And the "main" activity that the application opens as the root activity in the task.

(b), fallback stack:
1. Concept:
When the current activity initiates another activity, the new activity is placed at the top of the stack with focus.
The previous activity did not disappear, but was saved in the fallback stack, when it was in a stopped state.
When the user presses the Back button, the current activity is ejected from the top of the fallback stack (the activity is destroyed), and the previous activity is restored.
Activity in the stack is not rearranged. Therefore, the fallback stack operation is the same as the last-in-first-out object structure.
When the user presses the Back button, the current activity is destroyed and the previous activity is restored. If the user continues to press the rewind button,
Then each activity in the fallback stack is ejected, and the previous activity is displayed,
Until the user returns to the main screen (or the activity that was run at the start of the task). When all activity is removed from the fallback stack,
The task is no longer there.
(c), Activity start mode:
Every interface in Android is an activity, and switching interface operations is actually an instantiation of multiple different activity.
The startup mode of activity in Android determines how the activity starts running.
A, Activity startup mode settings:
<activity android:name= ". Mainactivity "android:launchmode=" standard "/>
B, activity of the four types of boot mode:
1. Standard (note: Standard is the system's default boot mode.) )
Standard startup mode, activity is created each time the activity is activated and placed in the task stack. The GetTaskID () of each form remains unchanged,
But This.hashcode () has changed.
2. Singletop
If there is an instance of the activity at the top of the task, the instance is reused and no new activity object is created.
However, it will call the Onnewintent () method.
If the top of the stack does not exist, a new instance is created and placed on top of the stack (even if the activity instance already exists on the stack, as long as it is not at the top of the stack, an instance will be created).
The Onnewintent () method is recalled.
3. Singletask
If an instance of the activity is already in the stack, the instance is reused (Onnewintent () of the instance is invoked). When reused, the instance is returned to the top of the stack,
So the instances above it will be removed from the stack. If the instance does not exist in the stack, a new instance is created to be put into the stack.
And Singletop in the name can see the difference, that is, singletop each time only detection of the current top of the activity is we need to create a request,
The Singletask detects all activity objects in the stack, from top to bottom,
If it detects what we are requesting, it will destroy the object above the activity object and put the detected activity to the top of the stack directly.
4. SingleInstance
The difference from Singletask mode is that the fallback stack that holds the SingleInstance mode window object cannot have any other window object.
Therefore, if the window does not exist, create a new task to hold the SingleInstance mode window.
That is, GetTaskID () will find that the task ID has changed. This startup mode works similarly to the browsers we use,
When accessing a browser in multiple programs, if the current browser is not open, the browser opens, otherwise it will be accessed in the currently open browser.
This mode saves a lot of system resources because he can guarantee that the activity object to be requested only exists in the current stack.

http://blog.csdn.net/u012702547/article/details/49529825
Three, stone scissors cloth small Game

Qianfeng-android-day05-activity life cycle, actitity start mode Basic Learning:

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.