Android – activities with single task startup mode and their location in the background stack

In the official online Android guide on tasks and back stack, I found a contradictory message. I would be grateful if someone could help me solve this problem

First of all, there are the following statements:

Then it describes the single task startup mode, with the following statements:

Well, if I understand it correctly, singletask activities can only be the root of their tasks, because they are created in this way, and activities will never exchange positions in the background stack. But how do such activities respond to receiving calls to the onnewintent () method?

There is another comment on this page:

Therefore, the guide points out that calling a singletask activity will move it forward in some way. But how is it possible if this activity is not the most important? Has the activity resumed, or just received a call to its onnewintent() method without showing it to the user? I really don't understand

PS: I asked almost the same question more than a year ago, but I didn't give the correct answer. Therefore, although my question is technically repeated, I think it's necessary to put it forward again, because it really deserves a clear and definite answer

resolvent:

I'll try my best to answer your question

first,

This is actually wrong. Usually, the activities in the stack will never be rearranged, but you can use intent.flag_ ACTIVITY_ REORDER_ TO_ Front brings existing activities to the front from other locations in the stack. For example, your activity stack is as follows: a - > b - > C - > D, where a is the root activity (at the bottom of the stack) and D is the top activity in the foreground (shown). If D does this now:

Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

This moves the existing instance of activity B to the front of the task so that it does not appear on the screen. The activity stack now looks like this: a - > C - > D - > B

Next you ask this:

Well, if I understand it correctly, singletask activities can only be the root of their tasks, because they are created in this way, and activities will never exchange positions in the background stack. But how do such activities respond to receiving calls to the onnewintent () method?

Typically, a singletask activity is the root of its task. If an existing task has activated a singletask activity and you start such an activity using startactivity(), the task will be advanced (for display on the screen) and onnewintent() will be in the existing instance of the activity. However, if a singletask activity has started other activities in its task, Then all other activities will be completed (that is, the task will be split back to its root activity) and before onnewintent() will be called on the existing singletask activity instance

In some cases, the singletask activity is not the root of its task, and then all bets are closed (i.e. the behavior is not recorded). This will happen if the singletask activity has the same taskAffinity as other activities already active in the task in the application. In this case, Android will ignore the singletask startup mode, The activity is considered to have a standard startup mode

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>