SingleInstance activity makes me confuse

Activity is a SingleInstance operation.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class A extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("A");
        startActivity(new Intent(this, B.class));
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        testLog("new Intent A");
    }

    private void testLog(String string) {
        Log.d("test", string);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        testLog("destroy A");
    }
}

      

Activity B is the standard activity.

In the normal case, A-> B. The action of the user and B is to destroy. The user clicks the "A destroy" button again. It is right.

But in another case: A-> B. User Action button. When the user restarts the task framework calls android A onNewIntent()

.

Why? I mean the action stack should look like this:

-B
-A

      

Why is android framework targeting A?

+2


source to share


3 answers


There are differences between singleTop

, singleTask

and singleInstance

. Read the documentation for more information, but you want to bookmark the description singleInstance

.

Allow only one instance of this action to run. This action gets a unique task, only works in it; if it is started again with the same intent, then this task will be transferred and the Activity.onNewIntent () method is called. If this activity tries to start a new activity, this new activity will be launched in a separate task. For details on tasks, see the Tasks and Trailing Stop document.



This is exactly the behavior you see and expect.

+3


source


Ok, now that you have answered this comment, I can answer your question feeling more secure.

Kabuko said he was right. However, you don't understand this because you don't even understand the concept of tasks and how it relates to the launcher.

When you have a launch singleInstance

in the launcher (see "why I asked about your manifest!"), The launcher does not start / restart all the actions that you started / used with Activity A (a singleInstance

activity). This is task A start / restartsince he stayed.

In fact, as the documentation says (read kabuko's answer), when you run B from A, B is considered a new task altogether. It just happens that these two actions are "glued" together in the activity history ... obviously, since you start B right after you start A. But again, I say it is important to understand that this represents a logical break in your application.



And why is this a logical gap, you ask?

Because A is activity singleInstance

. And, as stated here, this is one and only one activity in this task. So, if you run this task again from the launcher, you end up with A again, not B, since B is another task that does not belong to A.

Compare with what you expected: if A was standard

, B would be started and considered in the same problem. Therefore, restarting task A will end at B, as expected.

Finally, as the doc says, this is why you basically shouldn't always use singleTask

and even less singleInstance

. This is not expected behavior. And since they are not for you, they will be even less for the user, everything else is the same.

+2


source


android:taskAffinity="" android:launchMode="singleInstance"

      

0


source







All Articles