Effects of Android taskaffinity on four kinds of launchmode

Source: Internet
Author: User

In Android, all activity of a application has an identical affinity ( intimacy, similarity) by default . This means that all activity of the same application tends to belong to the same task. But we can't say that an app in Android has only one task stack. The author today for when taskaffinity different, four kinds of launchmode under the opening of a new activity will build a new task stack to do the experiment.

The basic code is as follows:

Androidmanifest.xml:

Mainactivity's Code

 PackageCom.example.testlaunchmode;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {Private Static FinalString TAG = "Mainactivity"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);          Setcontentview (R.layout.activity_main); TextView TV=(TextView) Findviewbyid (r.id.tv); Tv.settext ( This. toString () + "\ntaskid=" + This. GetTaskID ()); }            Public voidonjump (view view) {Intent Intent=NewIntent ( This, Activitytest.class);        StartActivity (Intent); } @Overrideprotected voidonnewintent (Intent Intent) {log.d (TAG,"Onnewintent:intent=" +intent); }       }

Activitytest's code is similar to Mainactivity, and I won't post it.

1. Standard mode:

Standard mode is the default mode. In this mode, activity can have more than one instance, and these instances can be in either the same task or a different task.

The code, as shown above, has no changes and the results are as follows:

After clicking the button

As you can see, the taskaffinity of Mainactivity and Activitytest are not the same, but they are placed in the same task stack.

2. Singletop mode

In singletop mode, if an instance of the activity exists in the same task, and the activity instance is at the top of the stack (that is, the activity is in the front end), when StartActivity () is called, the An example of the activity is no longer created; it simply invokes the activity's onnewintent (). Otherwise, a new instance of the activity is created and placed on top of the stack.

Just change the androidmanifest.xml file:

Operation Result:

After clicking the button:

As you can see with standard mode, although mainactivity and activitytest are taskaffinity different, they are still placed in the same task stack.

3.singletask Mode:

This mode I want to first paste code and results:

Operation Result:

It is obvious from the above results that activitytest is placed on a new task stack when opened and becomes the stack-bottom element of the new task stack, the root activity described in the document. At this point the latest task stack becomes the only foreground task stack, and the original mainactivity task stack becomes one of the many background task stacks.

Experiment to do this I have a new question: If the taskaffinity of two activity is the same, will it be like this?

So I changed the taskaffinity of two activity into the same operation again:

Results:

Obviously such results and the document given in the-a " singleTask " activity would always be the root activity of the task-is inconsistent. Later, the author finds such a passage in the document (set the FLAG_ACTIVITY_NEW_TASK flag): As described earlier, a new activity is, by default, launched into the task of the ACTI Vity that called startActivity() . It ' s pushed onto the same stack as the caller. However, if the Intent object passed startActivity() FLAG_ACTIVITY_NEW_TASK to contains the flag, the system looks for a different task to house the New activity. Often, as the name of the the flag implies, it ' s a new task. However, it doesn ' t has to be. If there ' s already an existing task with the same affinity as the new activity, the activity is launched into that task. If not, it begins a new task.

Documentation This is the paradox of red fruits. ~quq

Good steak, summed up is this:

To start with a, B.
(01) When the taskaffinity of A and B are the same: When an instance of B is created for the first time, the new task is not started, but instead is added directly to the task where a is located, and when an instance of B already exists, all activity above B in the task where B is located is deleted. b becomes the top element of the stack, which realizes the function of jumping to B.
(02) When the taskaffinity of A and B are different: When an instance of B is created for the first time, a new task is started, then B is added to the newly created task, and when an instance of B is introduced, all activity above B in the task of B is deleted. b becomes the top element of the stack (also the root Activity), which implements the function of jumping to B.

5. SingleInstance mode

SingleInstance, as the name implies, is a single instance meaning that only one instance of activity is allowed at any moment!
According to the documentation, only one instance of the activity is allowed in this mode. When you create the activity instance for the first time, a new task is created and the activity is added to the task. Note: This task can only hold the activity instance, and no additional activity instances will be added! If the activity instance already exists in a task, jump directly to that task.

Obviously this is consistent with the description of the document ~

Note: There may be more than one instance of activity that is started in standard and singletop mode, but both Singletask and singleinstance are only one instance. The difference is:

1.A "" Activity is always at the top of the singleInstance stack (since it's the only activity in the task). (Both the top and bottom of the stack)

2.However, a " singleTask " activity may or could not be activities above it in the stack. (There may be other activity on the top, not necessarily root activity)

Effects of Android taskaffinity on four kinds of launchmode

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.