The activity of a single android is not alone if the "Back" button is pressed

I came across an interesting problem when an Activity is created several times, even if it is defined as singleTask or singleelInstance Activity in the manifest. Here's how to reproduce it. Say, in the main activity:

@Override
protected void onResume() {
    Intent i = new Intent(MainActivity.class, SingleActivity.class);
    startActivity(i);
}

in my SingleActivity, I have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    Log.i("SingleActivity", "onCreate " + System.identityHashCode(this));
    ...
}

and in the manifest I:

    <activity android:name=".SingleActivity"
              android:launchMode="singleInstance"
    />

Now, if I run the application, everything looks fine, expect in one case: if I press the back button and SingleActivityis in front, it will go back to MainActivitywhere it MainActivity.onResume()will create another instance SingleActivityinstead of pushing the one that already exists. this is what I know because a different identifier hash is displayed in the log.

the same seems true if singleTask startup mode.

onBackPressed(), .

,

+5
2

BACK () .

,

, ,

.

+3

taskAffinity. taskAffinity MainActivity, SingleActivity, 2 ( ) taskAffinity. , Android taskAffinity , . taskAffinity , launchMode="singleInstance" launchMode="singleTask" ( , Android ) .

, , taskAffinity launchMode.

singleTask singleInstance ( , , , , ), singleInstance singleTask <activity>:

android:taskAffinity=""

, StackOverflow Google "startmode taskaffinity"

+4

All Articles