Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of taskAffinity attribute in android

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the role of taskAffinity attribute in android". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "what is the role of taskAffinity attribute in android"!

First, we launch application1, load Activity1, and press Home to bring the task (assuming task1) into the background. Then start application2, loading Activity2 by default.

What do we see? Yes, Activity2 should be displayed, but we see Activity1 instead. Activity2 is actually loaded, but Activity1 is rehosted, so Activity1 is seen.

The second scenario. If the intent of an Activity is loaded and Flag is set to FLAG_ACTIVITY_NEW_TASK, it will first check if there is a Task identical to its taskAffinity, if there is, it will be hosted directly into the Task, if not, it will recreate the Task.

Let's do a test.

Let's start by writing an app that has two activities (Activity1 and Activity2). AndroidManifest.xml looks like this:

The code for Activity2 is as follows:

public class Activity2 extends Activity {

private static final String TAG = "Activity2";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main2);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

Intent intent = new Intent(this, Activity1.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

return super.onTouchEvent(event);

}

}

Then, let's write another app MyActivity, which contains an Activity (MyActivity), AndroidManifest.xml as follows:

We start MyActivity first, then press the Home key to go back to the desktop, then open Activity2, click Activity2 to go to Activity1. Then press the Back key.

We found that we entered Activity2->Activity1 and returned Activity1->MyActivity. This illustrates a problem. Activity1 rehosts to the Task where MyActivity is located when it starts.

The above is a validation of the two proposed uses of TaskAffinity in the documentation.

Here's the magic moment, folks, don't blink!

We now combine launchMode from the previous article with taskAffinity from this article.

The first is the combination of the singleTask loading pattern and taskAffinity.

We still use the singleTask code in the previous article, which is not listed here. Please refer to the previous article for yourself. *** The difference is that we set MyActivity and Activity1 to the same taskAffinity and rerun the test above.

We found the results surprising: launching a singleTask from the same application and launching a different application resulted in exactly the opposite of what we saw above!

After some reflection, we can combine execution from the same application with execution from different applications and come to a conclusion:

When an application loads an Activity in singleTask mode, the Activity first checks to see if there is a Task identical to its taskAffinity.

If it exists, check if it is instantiated, if it is already instantiated, destroy the Activity above this Activity and call onNewIntent. If not instantiated, the Activity instantiates into the stack.

2. If it does not exist, then re-create the Task and merge it into the stack.

This is represented by a process:

Then we will test the situation when the singleInstance pattern is integrated into taskAffinity. We also use the example of testing singleInstance above, which is not listed here. The reader will refer to the previous article. *** The difference is that we set MyActivity and Activity2 to the same taskAffinity.

We also found some discrepancies in the test results, that is, when launching an Activity from a singleInstance, instead of recreating a Task, we entered the Task where the MyActivity has the same affinity as it.

We can also conclude that:

1. When an application loads an Activity in singleInstance mode, if the Activity is not instantiated, then a Task is re-created and incorporated into the stack. If it has been instantiated, then the onNewIntent of the Activity is called.

2. The Task in which the Activity of singleInstance is located does not allow other activities. Any other Activivities loaded from this Activity (assuming Activity2) will be placed in other Tasks. If there is a Task with the same affinity as Activity2, Activity2 will be created within this Task. If it does not exist, a new Task is generated and merged into the stack.

Thank you for reading, the above is "android taskAffinity attribute what role" content, after the study of this article, I believe that we have a deeper understanding of android taskAffinity attribute what role this issue, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report