Android Basic notes (eight)-Activity life cycle and task stack

Source: Internet
Author: User

    • Life cycle of activity
    • Screen switching problem
    • Concept of the task stack
    • 4 Startup modes for activity

Life cycle of activity

Activity throughout the life cycle as shown below, this picture is stripped from the Android API, I think the API on the life cycle has been explained in detail, I will not wordy, briefly say some of their own summary:

Three key rings that you might be interested in doing your job
① full life cycle
② visible life cycle
③ Interactive life cycle
, the cycles in the diagram are large, including small:
Use in the actual work
①onresume visible, can be interacted with. Start the dynamic refresh operation.
the
②onpause section is visible and cannot be interacted with. The dynamic refresh of some operations, to pause.
③oncreate Initializes a number of data
④ondestroy to release the data, saving memory.
Screen switching problem

When switching between screens, the activity is destroyed and recreated by default, and in applications like mobile games, this experience is very poor. To not allow activity to be destroyed while switching between the screen and landscape, just configure several properties of the node when the manifest file declares activity <activity> , as follows:

android:configChanges="orientation|keyboardHidden|screenSize"
Explain
①configchange= "orientation" screen orientation change: Do not let the screen re-create activity when switching
②sreensize Screen Size
③keyboardhidden is a soft keyboard, if you switch the screen, the soft keyboard will determine whether the screen size is appropriate to display the soft keyboard, in the process of judging the activity will be restarted
Setting a horizontal or vertical screen
android:screenorientation= "Portrait" Vertical Screen
android:screenorientation= "Landscape" horizontal screen
Concept of the task stack
The task stack is designed to enhance the user experience:
(1) When the program opens, a task stack is created to store the activity of the current program, and all activity belongs to a task stack.
(2) a task stack contains a collection of activity, which is ordered to select which activity interacts with the user: only the activity at the top of the task stack can interact with the user.
(3) The task stack can be moved to the background, and the status of each activity is preserved. It also lists their tasks in an orderly manner and does not lose their status information.
(4) When exiting the application: when all the activity in the task stack is purged from the stack, the task stack is destroyed and the program exits.
Disadvantages of the task stack:
(1) Each time the page is opened in the task stack to add an activity, and only the task stack of activity all clear out of the stack, the task stack is destroyed, the program will exit, so that the use, the user experience is poor, you need to click Multiple return to the program to quit.
(2) Each time a page is opened, adding an activity to the task stack can also result in data redundancy, too much data duplication, and a memory overflow problem (OOM).

In order to solve the problem caused by the task stack, Android designed the startup mode for activity, then the following content will introduce the activity in Android startup mode, which is one of the most important content.

4 Startup modes for activity

The startup mode (Launchmode) plays an important role in the process of multiple activity jumps, which can resolve whether to generate new activity instances, reuse existing activity instances, and share a task stack with other instances. The task stack is an object with a station structure, a task stack can manage multiple activity, and each launch of an application will create a corresponding task stack.

The activity has the following four types of Launchmode modes:
①standard
②singtop
③singtask
④singleinstance

We can configure <activity> the Android:launchmode property in Androidmanifest.xml for one of the above four kinds.

in order to explain the startup mode in detail , we created a project with two activity, respectively, FirstActivity and SecondActivity the startup mode standard . Here is the interface and key code:
Interface One

Interface Two

Firstactivity file:

 @Override  protected  void  onCreate     (Bundle savedinstancestate) {super . OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_first); System.out.println ( "current task Stack ID:"  + gettaskid () +  "is currently the first interface (" + this . toString () +);  @Override  protected  void  ondestroy     () {super . OnDestroy (); System.out.println ( "First interface ("  +this . toString () + ");  

Secondactivity file

 @Override  protected  void  onCreate     (Bundle savedinstancestate) {super . OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_second); System.out.println ( "current task Stack ID:"  + gettaskid () +  "is currently the second interface (" + this . toString () +);  @Override  protected  void  ondestroy     () {super . OnDestroy (); System.out.println ( "second interface ("  +this . toString () + ");  
Standard detailed explanation
Overview : In this mode, a new instance is generated, regardless of whether there are any existing instances. That is, each call to StartActivity () starts with a new activity placed on top of the stack and can be created repeatedly.
Action : After you start the program, do the following ① activate the second interface, activate the first interface, activate the second interface, activate the
first interface, activate the second interface
② Crazy Press the back button
The printed log is as follows :

the scenario in the stack is as follows:

Each activation creates a new instance, and each time it is returned, the instance is destroyed and the stack goes out

Singletop detailed explanation
Overview : If the activity on the stack at the top of the task stack exists, it will not recreate the activity, but instead reuse the existing activity. Ensure that the top of the stack is not created repeatedly if it exists.

In order to explain more clearly, I added the following log in the button

publicvoidfirst(View v) {    new Intent(this, FirstActivity.class);    startActivity(intent);    System.out.println("想要激活第一个页面");}publicvoidsecond(View v) {    new Intent(this, SecondActivity.class);    startActivity(intent);    System.out.println("想要激活第二个页面");}
Action: Perform the operation after start ① activate the second interface, activate first interface, activate first interface, activate first interface

② Press three times to return the key
the printed log is as follows :

the scenario in the stack is as follows:

Note that when the first page is on top of the stack and you also want to activate the first interface, the new instance will not be created because the singleTop schema no longer creates it, but if the first interface is no longer the top of the stack, a new instance is created.

Singletask detailed explanation
Overview : Only one instance exists in the current task stack, and when the activity is activated, check to see if an instance exists in the task stack and reuse the existing activity if there are instances. and empty all other activity on the activity, reusing the existing activity. Ensure that only one instance of the entire task stack exists;
action: Perform operation after startup ① activate the first interface, activate the second interface, activate the second interface,
activate the first interface
② Press the return key once
The printed log is as follows :

The stack structure here is out of the picture, so I'll describe the log.

application on, the first log is typed, and then click "Activate the first interface", because there are already instances in the stack, so there is no new instance created, direct reuse. Then click "Activate Second Interface", go to the second interface, and create an instance object, and then click "Activate the Second interface", there is an instance object created, click on "Activate the first interface", this will check whether the task stack has the first interface instance, found that there are instances, direct reuse, and the first interface stack above the two second interface instance directly out of the stack.
finally click the Back button, because there is only one instance of the first interface left in the stack, so only one log is printed once.
SingleInstance detailed explanation
Overview : This startup mode is special because it enables a new stack structure, places the activity in the new stack structure, and guarantees that no other activity instances will enter.
action: Perform operation after startup ① activate the second interface, activate the
first interface
② Press 3 times to return the key
The printed log is as follows :

the scenario in the stack is as follows:

As stated in the overview, when the app starts, it singleInstance creates a new task stack directly to the first activity, with the pattern configured for the first interface, and ensures that no other activity is in the stack, and that the reuse of the instance is guaranteed. Then to activate the second activity, since the second interface is the standard mode, a new instance is created directly and merged into the stack.

Android Basic notes (eight)-Activity life cycle and task stack

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.