Activity Startup Mode --- Summary

Source: Internet
Author: User

Activity has four startup modes:

1. Standard (standard) 2. singletop 3. singletask 4. singleinstance

Identifies the Startup Mode of an activity in two ways:

1. One is through androidmanifest. xml 2. The other is identified by intent

It is identified by androidmanifest. xml:

  1. <Activity Android: Name = ". activity1"
  2. Android: launchmode = "standard"
  3. Android: Label = "@ string/app_name">
  4. <Intent-filter>
  5. <Action Android: Name = "android. Intent. Action. Main"/>
  6. <Category Android: Name = "android. Intent. Category. launcher"/>
  7. </Intent-filter>
  8. </Activity>

It is identified by the flag of intent:

Flag_activity_new_task

Flag_activity_single_top

Flag_activity_clear_top

Certificate -------------------------------------------------------------------------------------------------------------------------------------

Standard Mode

"standard"(The default mode)
Default. the system creates a new instance of the activity in the task from which it was started and routes the intent to it. the activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.

For example, when activity1 is started in standard mode, no matter whether the current task contains activity1 or whether activity1 exists in the current stack, in short, the system creates a new activity1 object.

Example of an experiment:

The activity settings in androidmanifest. XML are as follows:

<Activity

Android: Name ="Com. testlaunchemode. activity. activitystandard"

Android: launchmode ="Standard"

/>

 

The Code is as follows:

Public ClassActivitystandardExtendsActivity {

PrivateTextview TV;

Public VoidOncreate (bundle state ){

Super. Oncreate (State );

Setcontentview (R. layout.Standard_activity);

MyApp APP = (MyApp) getapplicationcontext ();

TV = (textview) findviewbyid (R. Id.TV _result);

TV. settext ("activity launched in standard mode, this is times result from application :"

+ App. increasetimes ());

}

Public BooleanOntouchevent (motionevent event ){

Intent intent =NewIntent (This, Activitystandard.Class);

Startactivity (intent );

Return Super. Ontouchevent (event );

}

}

Each time you click the activitystandard interface, a new activitystandard object is generated again.

This can be confirmed by pressing the return button. When we press the return button, we will continuously return to the previous activity, and the appearance of the previous activity is the same as that of activitystandard. As a result, we can conclude that activitystandard is constantly created.

If you click it four times and the activitystandard is not triggered by clicking it when it comes in, in the task, in the stack:

Activitystandard

Activitystandard

Activitystandard

Activitystandard

Activitystandard

Bytes ----------------------------------------------------------------------------------------------------------

Singletop Mode

If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to itsonNewIntent()Method, rather than creating a new instance of the activity. the activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack isNotAn existing instance of the activity ).

For example, suppose a task's back stack consists of Root Activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top ). an intent arrives for an activity of Type D. if D has the default"standard"Launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D's launch mode is"singleTop", The existing instance of D sort es the intent throughonNewIntent(), Because it's at the top of the stack-the stack remains A-B-C-D. however, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is"singleTop".

Note:When a new instance of an activity is created, the user can pressBackButton to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot pressBackButton to return to the State of the activity before the new intent arrived inonNewIntent().

For example, activitysingletop is set to singletop in startup mode,

Next, the top of the current stack is an activity of the activitysingletop type. Then, if activitysingletop is to be started for other activities, the system will not recreate the activitysingletop object, instead, the activitysingletop object located at the top of the stack is used directly.

That is to say, if the Startup Mode of the activity to be started is singletop and the activity already exists at the top of the current stack, the system will not create a new activity, this is different from standard startup mode.

Example:

<Activity

Android: Name ="Com. testlaunchemode. activity. activitysingletop"

Android: launchmode ="Singletop"

/>

Code:

Public ClassActivitysingletopExtendsActivity {

PrivateTextview TV;

PrivateMyApp;

Public VoidOncreate (bundle state ){

Super. Oncreate (State );

Setcontentview (R. layout.Singletop_activity);

Incircle ();

}

Private VoidIncircle (){

TV = (textview) findviewbyid (R. Id.TV);

MyApp = (MyApp) getapplicationcontext ();

TV. settext ("singletop launchmode, times:" + MyApp. increasetimes ());

}

Public BooleanOntouchevent (motionevent event ){

Intent intent =NewIntent (This, Activitysingletop.Class);

Startactivity (intent );

Return Super. Ontouchevent (event );

}

@ Override

Protected VoidOnnewintent (intent ){

Settitle ("I am activity1 too, but I called onnewintent! ");

Super. Onnewintent (intent );

}

}

Effect:

First entry:

At this time, the objects at the top of the stack Are activitysingletop objects, that is, the current activity:

Then, when I touch the current activity multiple times, the following times remain 0:

This indicates that no new activitysingletop object is created when activitysingletop is started, but the activitysingletop object currently at the top of the stack is used. The system passes the intent sent by the caller as a parameter to the onnewintent method of the activitysingletop object at the top of the stack.

Note: If the called activity (in singletop Startup Mode) is not at the top of the stack, the above effect is not met.

Then, I press the return key to directly return to other activities.

Bytes -----------------------------------------------------------------------------------------------------

Singletask Mode

"singleTask"
The system creates a new task and instantiates the activity at the root of the new task. however, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent()Method, rather than creating a new instance. Only one instance of the activity can exist at a time.

Note:Although the activity starts in a new task,BackButton still returns the user to the previous activity.

As another example, the android browser application declares that the Web browser activity shoshould always open in its own task-by specifyingsingleTaskLaunch mode in<activity>Element. This means that if your application issues an intent to open the android browser, its activity isNotPlaced in the same task as your application. instead, either a new task starts for the browser or, if the browser already has a task running in the background, that task is brought forward to handle the new intent.

Regardless of whether an activity starts in a new task or in the same task as the activity that started it,BackButton always takes the user to the previous activity. However, if you start an activity that specifiessingleTaskLaunch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground. at this point, the back stack now has des all activities from the task brought forward, at the top of the stack. figure 4 extends strates this type of scenario.

Figure 4.A representation of how an activity with launch mode "singletask" is added to the back stack. if the activity is already a part of a background task with its own back stack, then the entire back Stack also comes forward, on top of the current task.

If the Startup Mode of the started activity is singletask, then:

1. If a task contains the activity, the intent is passed to the onnewintent () method of the activity.

2. If no task contains the activity, the system creates a new task, and then the newly created activity instance serves as the root activity of the new task.

Example 1: the activity to be started already exists in the current task.

Start activitysingletask for the first time:

Then, start another activity:

Then start activitysingletask:

As you can see, if the activitysingletask to be started is already in the task, the system will pass the intent to this

The activitysingletask instance in the task does not create a new activitysingletask instance.

Therefore, there is only one activitysingletask instance in the current task. Therefore, singletask in startup mode can be interpreted,

In the task where the activity is located, only one instance exists for the activity.

Example 2: In the same application, the activitysingletask instance does not exist in the current task.

First, start a standard activity:

Then, start a singletask activity that does not exist in the current task:

Taskid can be used to determine that the two activities are located in the same task. In fact, in the same application, if the current task does not have an activitysingletask2 instance, the system does not create a new task to store the activitysingletask2 instance. The system only stores the created activitysingletask2 instance in the current task.

[The system creates a new task and instantiates the activity at the root of the new task]

In this document, if a singletask activity in the startup mode is in the same application as the initiator, then it will not be true.

Example 3: The singletaskactivity to be started is in a different application than the initiator.

First, create an app that contains the singletaskactivity to be started:

Then, in another app, start the cactivity in oneapp:

Then, the call result is:

Then, the bactivity and Aactivity will be returned continuously. Both the two activities are in the same task as the cactivity.

From the above results, we can know that if the started activity is in singletask mode and is in other applications,

The started activity and started activity belong to different tasks. In addition, if the task where the singletask activity is located is in the background state, the entire task is changed to the forward state.

As shown in:

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------

Singleinstance Mode

Same"singleTask", Could t that the system doesn't launch any other activities into the task holding the instance. the activity is always the single and only member of its task; any activities started by this one open in a separate task.

If an activity is set to singleinstanceactivity in singleinstance mode, a new task is created after singleinstanceactivity is started, which is used only to store singleinstanceactivity instances.

In addition, when singleinstanceactivity starts other activities, other activity instances are placed in a new task and will not be stored in the same task as singleinstanceactivity. That is to say, only singleinstanceactivity is an instance in a task.

Example:

First, start the singleinstanceactivity:

Then, start another activity:

Then, start singleinstanceactivity:

From the above we can see that singleinstance exclusively occupies one task. Then, if the system already has a task that already contains an activitysingleinstance instance, then, the system, the intent that starts activitysingleinstance is passed to the instance. At this time, the onnewintent method of the instance is called and no new activitysingleinstance instance is created.

Bytes ------------------------------------------------------------------------------------------

Activity Startup Mode --- Summary

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.