The taskAffinity attribute in Android and notes on the use of singleTask

We must first have the concept of task. We can think of task as a collection of activities that store runtime activities, that is, a task stack system that stores activities in our app and stores activities in the order in which they are started.

And taskAffinity is called attribution, which can be understood as the meaning of grouping activities. It will "adsorb" the same activities into a new task stack. Some people may not understand this attribute like me and can't see where it has been used. It, in fact, each Activity has a default taskAffinity attribute, we can set the taskAffinity uniformly in the <application/> of the manifest, or specify it separately in a single <activity/>.

So what is it good for? We can demonstrate it with an example.

We first create the Demo1 project, which contains Activity1 and Activity2, and we specify the taskAffinity of Activity2 as "com.yang.test1":

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity2"
            android:taskAffinity="com.yang.test1" />

The code of Activity1:

public class Activity1 extends Activity {

    TextView textView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.act_tv);
        textView.setText("Activity1");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Activity1.this, Activity2.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Pay attention to this flag first
                startActivity(intent);
            }
        });
    }
}

Then we are creating Demo2, also Activity3, and Activity4:

        <!--taskAffinity has the same name as Demo1-->
        <activity android:name=".Activity3"
            android:taskAffinity="com.yang.test1">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity4"
            android:taskAffinity="com.yang.test1" />

Code in Activity3:

public class Activity3 extends Activity {

    TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.act_tv);
        textView.setText("Activity3");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Activity3.this, Activity4.class);
                startActivity(intent);
            }
        });

    }
}

The codes of Activity2 and Activity4 will not be posted, and it is enough to distinguish them when displayed.

Well, after writing so much boring code, we will start the demonstration. We first start Demo2 and enter Activity3 by default, and then enter Activity4 by clicking events. At this time, we return Demo2 to the background, then we start Activity1 of Demo1, and then click Jump to Activity2.

You may feel that it is no different from usual here, but when we click the return button, you will find that the original return order is: Activity2->Activity1, which becomes Activity2->Activity4->Activity3->Activity1, is it a bit strange? ?

In fact, it is because the activities in our two demos have the same taskAffinity. When starting the activity with FLAG_NEW_TASK, the system will look for the same taskAffinity as it in the started activity. If there is, it will push these activities into the foreground task and put itself Push the top of the Task and display it, if not, create a new Task and push itself onto the stack.

When Activity1 jumps to Activity2 in the above Demo2, the system searches for the same taskAffinity as it. At this time, the system finds that Activity3 and Activity4 are the same taskAffinity and have been started , and then add these two Activities to the Task of Demo2 first, and finally add Activity2 Add to Task.

Here is a practical example. I believe everyone has done push. When the user clicks the notification bar message, we need to add FLAG_NEW_TASK to the Intent to jump to the specific push interface. If the App has already been started, when you click the return button, you will You will find that the previously launched interface will be displayed,

That is to say, the system moves the Task of the Activity of the same taskAffinity to the foreground. We have mentioned earlier that Activity is set with taskAffinity by default, so if it is not modified, the taskAffinity of Activity is the same, so when we click push, we jump not only to the push interface, but also to the set of activities that have been started.

We can summarize:

1. Each Activity has a default taskAffinity property

2. taskAffinity specifies the stack to which the Activity belongs. By default, if it is not set, the App has only one stack by default.

3. If FLAG_NEW_TASK is added to the intent when jumping to the interface, the system will move the Activity that has been started and has the same taskAffinity attribute to the foreground, that is to say, a new stack may be opened and the same taskAffinity attribute Activity in different apps will be "adsorbed" into it. stack.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324926043&siteId=291194637