Four modes of loading for activity

Source: Internet
Author: User

Introduction to Startup mode: *standard: Standard mode, which is also the default load mode.    *singletop:task Stack Top single case mode.    *singletask:task in single-case mode. *singleinstance: Global singleton mode. First, Standard mode start: When started by standard mode, the system always creates a new instance for the target activity and adds the activity to the current task stack--This startup mode does not start a new task.    The new activity will be added to the original task stack. Example: To create an activity that repeatedly loads the activity in standard mode, you can see that the ID of the task is constant-indicating that the load pattern does not use the new task, but rather uses the same task stack. When we press the Back button, the system will delete the activity from the top of the activity stack "individually".
public class Standardstartactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
LinearLayout ll=new LinearLayout (standardstartactivity.this);
Ll.setorientation (linearlayout.vertical);
This.setcontentview (LL);
This textview is used to display activity and task IDs
TextView textview=new TextView (standardstartactivity.this);
Textview.settext ("Activity is:" +this.tostring () + "\ n" + ", the task ID is:" +this.gettaskid ());
Set a button to start mainactivity
Button Button=new button (standardstartactivity.this);
Button.settext ("Start mainactivity");
Adding TextView and buttons
Ll.addview (TextView);
Ll.addview (button);
Set the listener for the button
Button.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
Intent intent=new Intent (standardstartactivity.this,standardstartactivity.class);
StartActivity (Intent);
}
});
}
}
Second, Singletop mode start:
    This pattern is similar to standard mode, but it is a bit different: when the target activity that is going to start is already at the top of the task stack, the system does not recreate the instance of the target activity, but instead directly re-uses the existing activity instance. If the target activity that will be started is not at the top of the stack, then the instance of the target activity is recreated.

Three, Singletask () mode:

    This mode of loading activity has only one instance within the same task, which can be divided into the following three scenarios when starting the target activity:
  * If the target activity you are about to start does not exist for you, the system will create an instance of the target activity and add it to the top of the task stack.
  * If the target activity that will be started is already at the top of the stack, this is the same behavior as the singletop mode.
* If the target activity to be started already exists, but not at the top of the task stack, the system will remove the task stack from all activity on top of the activity so that the target activity is transferred to the top of the stack.
Example:
Create an activity that is set to apply portal activity and start in the default mode. and set a button in this activity to jump to another activity that starts in Singletask mode
public class Singletaskmodleactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
LinearLayout linearlayout=new LinearLayout (singletaskmodleactivity.this);
Linearlayout.setorientation (linearlayout.vertical);
This.setcontentview (LinearLayout);
TextView textview=new TextView (singletaskmodleactivity.this);
Textview.settext ("Activity is:" +this.tostring () + "\ n" + ", the task ID is:" +this.gettaskid ());
Button Button=new button (singletaskmodleactivity.this);
Button.settext ("Start onother_activity");
Linearlayout.addview (TextView);
Linearlayout.addview (button);
Button.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
Intent intent=new Intent (singletaskmodleactivity.this,onotheractivity.class);
StartActivity (Intent);
}
});
}
}
Create another activity and start in Singletask mode (that is, configure <activity in Androidmanifest.xml
Android:launchmode= "Singletask"
 > This property) also sets a button in this activity to control jump to singletaskmodleactivity 
 public class Onotheractivity extends Activity {
@Override
protected void onCreate (Bundle savedinstancestate ) {
Super.oncreate (savedinstancestate);
LinearLayout linearlayout=new linearlayout (onotheractivity.this);
Linearlayout.setorientation (linearlayout.vertical);
This.setcontentview (LinearLayout);
TextView textview=new TextView (onotheractivity.this);
Textview.settext ("activity is:" +this.tostring () + "\ n" + ", the task ID is:" +this.gettaskid ());
Button Button=new button (onotheractivity.this);
Button.settext ("Start singletaskmodle_activity");
Linearlayout.addview (TextView);
Linearlayout.addview (button);
Button.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
Intent intent=new Intent (onotheractivity.this,singletaskmodle.class);
StartActivity (Intent);
}
});
}
}
      (1)  when we run the program, enter singletaskmodleactivity the first time click the button "Start Onother_activity", this time will jump to onotheractivity,
Onotheractivity will start in Singletask mode: The onotheractivity does not exist, so the system will create a new activity instance. The activity that exists now is from bottom to top:
Singletaskmodleactivity→onotheractivity
(2) Through the above steps, our Activity is onotheractivity, click the button "Start singletaskmodle_activity" jump to Singletaskmodleactivity, Since Singletaskmodleactivity is started in the default mode (standard), a new activity instance is created: At this point the activity is from bottom to top:
Singletaskmodleactivity ... 1→onotheractivity→singletaskmodleactivity ... 2
(3) Through the above steps, in the Singletaskmodleactivity, at this time click the button "Start onother_activity" jump to Onotheractivity, and to Singletask
Mode is started, there is already a onotheractivity present in the second layer is not on the top of the stack, so when it is started it will be the second layer above all the activity (now only singletaskmodleactivity ... 2) Remove the task stack and let the onotheractivity go to the top of the stack. The activity that exists at this time is from bottom to top:
Singletaskmodleactivity→onotheractivity
Four, SingleInstance (single instance) load mode:
    In this load mode, the system guarantees that the target activity will be started regardless of which task, only one target activity instance is created and a new task stack is used to load the activity instance.
    * If the target activity that will be started does not exist, the system creates a completely new task, creates an instance of the target activity, and adds it to the top of the new task stack.
    * If the target acticity to be started already exists, regardless of which application it is in, and in which task stack, the system will forward the activity's task to the foreground, so that activity is displayed.
V. Some application scenarios:
    Singletop: The content Display page that is suitable for receiving notification launches. For example, a news client's news content page, if it receives 10 news feeds, is annoying to open a news content page each time.
May jump to an interface multiple times from outside
Singletask: Suitable as a program entry point. For example, the browser's main interface. Regardless of how many apps launch the browser, only the main interface is started once, the rest will go onnewintent, and the other pages above the main interface will be emptied.
SingleInstance: For pages that need to be separated from the program. such as alarm reminders, separate alarm alarms from alarm settings. SingleInstance do not use in the middle page, if used in the intermediate page, jump transfer problems, such as: A-B (singleinstance)-C, after the full exit, this start, first opened is B. (Google Maps used in an app, when you exit the app, go to Google Maps, or just the interface)



Four modes of loading for activity

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.