Android Startup mode Detailed introduction _android

Source: Internet
Author: User
Tags close page

Android Startup mode:

Startup mode: Launchmode plays an important role in the process of multiple activity jumps, and it can decide whether to generate new activity instances, reuse existing activity instances, and share a task with other activity instances.

Here is a brief introduction to the concept of Task stack task, which is an object with a stack structure, a task can manage multiple activity, start an application, and create a task corresponding to it. The activity in the task stack is kept in the form of advanced, such as: you open a page, open another page in the page, another page exit, return is the first open page, this is the task stack simple original.

But in the actual program many times after the page jump, do not want to go back to the last open page, this time the page to display the starting mode is very necessary.

Activity altogether has the following four kinds of Launchmode boot mode:

1.standard
2.singleTop
3.singleTask
4.singleInstance

We can configure the Android:launchmode property in the Androidmanifest.xml to one of the above four types. As shown in the figure:

These four startup modes are described separately below.

First understand the concept of the top of the stack: as long as the page just opened, no matter what mode he is, it is the top of the stack page.
The concept of the bottom of the stack although the book does not say, but to understand the pattern of the thorough stack, or to know that there is such a statement, such as: The first open page is located at the bottom, and then open the next page on the heap. Only the bottom page closes and the program is closed.

A The concept and understanding of four startup modes

(i) standard standard mode

is the default boot mode, and you can also specify a value of standard instead of configuring the Android:launchmode property.
Every time you start an activity in standard mode, you create an instance of the task and put it into the stack;
Simple understanding: If several pages are standard mode to start, then the page whether jump to another page or jump to their own page, jump how many times, it is necessary to click the number of exit keys to close all the last.

(ii) Singletop stack top single case mode

Specifies that the property android:launchmode= "Singletop" and the system will process the jump behavior according to the Singletop startup mode. This is not commonly used in practice.
If an activity activates itself, that is, at the top of the task stack, it does not need to be created, simply by reusing an existing activity example. But if you are not the top of the stack, you will create your own example.
Simple understanding: If the page's startup type is singletop type, it clicks to jump to its own page, only need to click a single exit to exit the program. Because when it is on the top of the stack, no matter how many times it clicks to jump to its own, there is an activity instance, click an exit will exit the program. Compared with the standard mode, the standard mode clicks how many times you have to quit before exiting the program.

(iii) Single case model in Singletask

      Specify properties Android:launchmode= "Singleletask", which is the more commonly used pattern in the application. It is also the mode that we want to focus on understanding.
      If the activity in the Singletask mode that you want to start is present in the task stack, you do not need to create it, just put it on top of the stack, And the activity instances above the activity are moved out of the stack; If an activity does not exist for the pattern, the activity that creates the pattern is placed on the top of the stack. In other words: A stack can only have a singletask mode of activity.
      above a few points to understand:
      If the activity of the Singletask mode already exists on the top of the stack, Then jump to your own page again, it will not create your own activity instance object, this and Singletop is the same.
      But if the activity of Singletask mode is no longer on the top of the stack, then jumps to its own page again, it does not create its own activity instance object, It also jumps directly to its own instance activity and removes the stack from all activity instances that are pressed on top of the pattern, but there is no way to remove the activity instance underneath the pattern.
      If the activity in the Singletask mode is not yet created, it creates a new activity instance object and puts itself on top of the stack, This is the same as for all activity instance objects.
      about Singletask application is particularly noteworthy, such as the design of the main page is generally used Singletask mode to design, because the user clicks the page after the mutual jump, in the click Back to the homepage , and then click Exit again, when his actual demand is to quit the program, rather than one at a time just jumped the page, and finally quit. This requires the use of Singletask mode.

(iv) SingleInstance Global single Case model

Specifies the property android:launchmode= "Singleleinstance".
In this mode, no matter which task stack task starts the activity, only one instance of the target is created, and a new task stack is used to load the instance.
It can be seen that the singleinstance mode is more overbearing than the Singletask mode, opening an activity in the Singletask mode, which, if it already exists, removes the activity instance on the task above it. and open the SingleInstance mode of activity, whether it exists or not, he will create a new task, put himself inside, also means that the newly opened singleinstance activity in the new stack of their own only one instance object.
But it and the new task will not remove the activity instances above or below the previous activity instance, and the activity in the previous task remains.
If you create a mainactivity instance in a 1 task stack, and if you use 2 to activate mainactivity, you do not need to create, and the two applications share the activity instance;
The singleinstance pattern is typically used for resource sharing. For example, there is software a open QQ and with software B open QQ is open is the same QQ software. And after exiting the QQ program, the original open program has not been closed.
This is the role of singleinstance mode.

Two Programming programs to enhance understanding

This program is just a simple page jump, used to verify the correctness of the model above and strengthen the understanding of the pattern process.

A simple copy of this program can be completed.

Design results: Four activity and four layout files, as shown in the figure:

(i) Design of layout document Activity.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:gravity=" Center "Android : orientation= "Vertical" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_conten" T "android:layout_gravity=" center "android:text=" standard mode "/> <button android:layout_width=" Wrap_co Ntent "android:layout_height=" wrap_content "android:onclick=" tomainactivity "android:text=" to the homepage "Android:t" Extallcaps= "false"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" "android:onclick=" tosingletopactivity "android:text= singletop Mode page" android:textallcaps= "false"/> <B Utton android:layout_width= "wrap_content" android:layout_height= wrap_content "android:onclick=" ToSingleTaskAc Tivity "android:text=" TosinglETask Mode page "android:textallcaps= false"/> <button android:layout_width= "Wrap_content" Android:layout_h eight= "Wrap_content" android:onclick= "tosingleinstanceactivity" android:text= "tosingleinstance Mode page" Android:te 
    Xtallcaps= "false"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:onclick= "Tofinish" android:text= "Close page" android:textallcaps= "false"/> </LinearLayout>

The contents of the TextView control above are changed to the schema name of the page design.

(ii) design of Mainactivity.java file code

Package com.example.android;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.os.PersistableBundle;

Import Android.view.View; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super
    . OnCreate (Savedinstancestate);
  Setcontentview (R.layout.activity_main);
  //Jump to mainactivity page public void tomainactivity (View v) {startactivity (The new Intent (this, mainactivity.class)); //Jump to singletopactivity page public void tosingletopactivity (View v) {startactivity (The new Intent (this, singletopact
  Ivity.class)); //Jump to singletaskactivity page public void tosingletaskactivity (View v) {startactivity (The new Intent (this, Singletaska
  Ctivity.class)); //Jump to singleinstanceactivity page public void tosingleinstanceactivity (View v) {startactivity (The new Intent (this, Sin
  Gleinstanceactivity.class));
  //Close page page public void Tofinish (View v) {finish (); }


  }

The file name of the four activity files can be changed.

(iii) before the operation of the program to set the Androidmanifest.xml, as follows:

<activity android:name= ". Mainactivity ">
  <intent-filter>
    <action android:name=" Android.intent.action.MAIN "/>

    <category android:name= "Android.intent.category.LAUNCHER"/>
  </intent-filter>
</activity >
<activity
  android:name= ". Singletopactivity "
  android:launchmode=" Singletop "/>
<activity
  android:name=". Singletaskactivity "
  android:launchmode=" Singletask "/>
<activity
  android:name=". Singleinstanceactivity "
  android:launchmode=" SingleInstance "/>

The previous activity was added by default, and the following three activity was added manually

(iv) Results after the operation of the program:


After the start of the program click on each mode to try, and finally each click on the close page to see if the order of exit is what you think? It's OK to know the process.

A few simple logical sequences, such as:

1. For example, click on the main Page button many times to click the Close Page button to exit the program.
2. Click Tosingletask mode of the page, no matter how many times you click, go to the homepage of the button or Tosingtop button, and then click a Tosingletask button, click the Close Page button, return is the standard mode page, Click the Close Page button again to exit the program.
3. You can also change the startup mode of the start page of the program, and then look at the effect of the operation.

(v) Compare these four modes

Android's four startup modes are standard, Singletop, Singletask, SingleInstance, which is a few of the key words we have to remember. What are the differences between these modes? Let me briefly describe:

1. From the naming of the four patterns, it can be seen that the following three modes have the single keyword, which is an example, so the following three patterns are single examples. The previous mode is the load mode, that is, how many times the page is clicked, how many activity objects the page will load.

2. The following three single case models can be compared, and they may be found to have a stronger single case strength than one:
(1) Singletop Single example mode, only to ensure that it is in the case of the top of the stack, it is a single case mode; For example, the current page is the singletop mode of the page, if click to jump to its own page, in the stack is not created again its activity object If the page in the Singletop mode has not yet been created or given to the pattern, the page that jumps to that mode will re-create its activity object if it is not on the top of the stack.

Comparing standard mode and Singletop pattern found that within the same stack, there can be multiple standard modes of the same page object, or there can be multiple singletop patterns of the same page object, However, the same page of the singletop pattern cannot exist twice in a row, and here the note is two consecutive times. The same page of the standard pattern can appear several times in a row, which is the difference between them.

(2) Singletask Single example mode, it can guarantee that it is only one activity object in the entire stack, if the same Singletask mode page was opened before, then click on the other page again, click on the top of the Singletask mode of the page, All of the activity objects behind the first opening of this page will be purged, but the activity object of the page opened before the Mode page is opened is not cleared.
Comparing Singletop and Singletask, the number of the entire stack is one of the differences, and the same page of the singletask pattern can have only one activity object in a task.

(3) SingleInstance single case mode, the above Singletask mode is already very strong, is there any stronger than him? There must be some answers. The SingleInstance single example pattern guarantees that there will be only one activity object in the entire task, and that it should be noted that the entire task, whether it is the bottom of the stack, is itself. The way to do this is to create a new task stack of its own to hold an example of the activity, and when clicked again it would not have created its own example, whether it was on the top of the stack, and it would save only one instance object throughout the program. Because it is saved by its own task stack, it does not erase its activity instance objects; For example, open other mode pages on a singleinstance-mode page, and its activity objects are stored in an original task stack, That is, the task stack before the SingleInstance page is opened, and if the page of the SingleInstance mode is opened at the beginning, then the other schema's activity object is placed in a new task stack.

Comparing Singletask and SingleInstance, these two single case patterns, the activity object of the Singletask pattern removes the activity object on top of its activity object, and the singleinstance pattern, Will save its own object in another task stack.

In these four modes, a page with only singleinstance mode creates a new task stack (the same page is only created once), and other patterns create the activity objects in the original stack

Three Android startup mode-related knowledge

(i) exit from a single activity method: 1. Call FINISH2. Kill the process: KillProcess (PROCESS.MID) 3. Terminating a running virtual machine: System.exit () (ii) Withdrawal of the entire application: 1. Manufacturing throws an exception causes the entire program to exit 2. Put all the activity into a list and then drop all the activity,finish when you need to exit 3. Complete exit by broadcasting

The Exit function is accomplished by broadcasting, which is done by registering a broadcast receiver for the activity at the time of each activity creation (OnCreate) and sending the broadcast when exiting.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.