Android-activity four-state, seven-life-cycle, and four-boot mode

Source: Internet
Author: User

Introduction to one or four basic components:
Android four basic components: Activity, Service, Content Provider, broadcast Receiver
Activity: Active view
A component that is responsible for interacting with the user's display interface.
Service: A service that has no interface and is running in the background.
Content Provider: An application can share its own data by using the contents Provider, and another app can access it through contentresolver when it wants to use the data provided by the app.
Broadcast receiver: Broadcast receiver: A broadcast message receiver that listens to an event source that is another component in an Android app.

Second, summarize
Android is the use of tasks to manage activities, a task is a set of activities placed in the stack, the stack is also called the return stack, whenever a new activity is started, it will be in the stack, and at the top of the stack, when an activity finish will destroy the activity, at the top of the stack activity will be out of the stack. The system displays always top-of-stack activity.

Iii. four states of activity
1) Operating state: When an activity is located at the top of the stack at the back of the stack, the activity is in the running state and the most unlikely to be reclaimed by the system is the running state activity .
2) Pause Status: When an activity is no longer at the top of the stack, but still visible, a popup dialog or an activity that does not fill the screen will cause the previous activity to be paused, and the system will not easily recycle such activity. Unless the memory is extremely low (recycling visible activity can result in a very bad user experience)
3) Stop state: When an activity is not at the top of the stack and is completely invisible, it goes into a stopped state and the system recycles such activity when the memory is low
4) Destroy state: When an activity is removed from the stack and then programmed to destroy the state, the system recycles such activity

Iv. seven life cycles of activities

1) onCreate (): Called when activity is first created, completes initialization of the activity.
2) OnStart (): Called when the user can see the activity
3) Onresume (): When the user's focus is obtained, the user taps the screen
4) OnPause (): Called when the system is ready to start or reply to another activity. In this method, some small CPU resources are freed and some important data is saved.
5) OnStop (): When the activity is completely invisible it is called when the new startup activity is in the dialog box and is still visible when the method is not called
6) OnDestroy (): Called when activity is destroyed
7) Onrestart (): Called when the activity has a stop state that becomes a running state.

Test:
Open a dialog-box activity:

Print:

When jumping to another activity:

Five or four startup modes
1) Standard: default mode, for activities that use standard mode, the system will create a new instance of the activity, regardless of whether the activity has already existed in the return stack.

Test:

The ID of the current mainactivity is 40f76d48
Click the first button: the ID changes to 40F8BDC0, indicating that a new activity has been created

2) Singletop: When the activity is already at the top of the stack, and then start the activity directly using it instead of creating an instance of the activity again, it is a good solution to the problem of repeatedly creating top-of-stack activities.

When you click Start singletop mode activity, jump to that interface, Id:40f979e8

Then click on the interface button, the ID has not changed, indicating that no new instances were created.

3) The Singletask:singletop mode can be a good solution to the problem of repeatedly creating top-of-stack activities, but when the activity is not at the top of the stack, it is possible to create multiple instances, which can be used in Singletask mode. When the activity mode is specified as Singletask mode, each time the activity is started, the system checks to see if the activity instance exists in the return stack, and if it exists, it is used directly, and is created without saving.

Test:
Click Start Singletaskactivity,id Yes: 40fcd3a0

Then click on the button of this interface, jump to the main interface, note that this is the singletaskactivity is not on the top of the stack, but still in the stack.

Repeat the first step, still jump to this interface, the ID has not changed.

4) SingleInstance: activities designated as SingleInstance mode will enable a new task stack to manage this activity. Activities in this mode are primarily intended for other programs to share the activity, such as the use of a browser.

Test:
Create a new app that contains an activity with SingleInstance mode, run with ID 40F78BC8

Then click Start SingleInstance Activity in the main interface, and the result is to jump from this program directly to the above application.

Vi. Summary
Knowing the life cycle of a good activity clearly knows what state the activity is currently in, so that we know what to do in what state. The starting mode of the activity has its own characteristics, there is no best, only the most suitable, need to choose the appropriate model according to the actual situation.

Source

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android-activity four-state, seven-life-cycle, and four-boot mode

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.