android – Bug: onNewIntent not called for singleTop activity with Intent.FLAG_ACTIVITY_NEW_TASK-ThrowExceptions

Exception or error:

There appears to be a bug in startActivity.

By setting activities to be singleTop with different taskAffinity in AndroidManifest.xml and using the Intent.FLAG_ACTIVITY_NEW_TASK when calling startActivity, two activities can be created in two tasks (one activity per task).

Calling startActivity again will return to the first activity/task and onNewIntent is called. However, calling startActivity a forth time will return to the second activity/task, but onNewIntent is not called.

The only difference between the two tasks is their taskAffinity. Somehow, asymmetrical behaviour is observed.

However, if the Intent.FLAG_ACTIVITY_SINGLE_TOP is also used, then onNewIntent is called as expected.

It would appear that singleTop in AndroidManifest.xml is not the same as Intent.FLAG_ACTIVITY_SINGLE_TOP in the Intent.

public class ActivityA extends Activity implements OnClickListener {
    private String tag;

    public void onCreate(final Bundle savedInstanceState) {

        tag = getClass().getName();
        Log.v(tag, "onCreate()");

        Button button = (Button)findViewById(;
        button.setText(tag.endsWith("ActivityA") ? "Activity B"
                : "Activity A");

    public void onClick(View v) {
        Intent intent;
        int flags = Intent.FLAG_ACTIVITY_NEW_TASK
        // | Intent.FLAG_ACTIVITY_SINGLE_TOP

        Log.v(tag, "onClick()");

        intent = new Intent(this,
                tag.endsWith("ActivityA") ? ActivityB.class
                        : ActivityA.class);

    protected void onNewIntent(Intent intent) {
        Log.v(tag, "onNewIntent()");
public class ActivityB extends ActivityA {

<?xml version="1.0" encoding="utf-8"?>

            android:label="Activity A">
                    android:name="android.intent.action.MAIN" />
                    android:name="android.intent.category.LAUNCHER" />

            android:label="Activity B"

How to solve:

this is a little bit late, but since i just came across this –
as Matt mentions, the flag activity single top in the manifest and in the intent are not the same (at least on previous versions of android).

in startActivityUncheckedLocked method:

on gingerbread –

look at line 2204 —

 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
                                && taskTop.realActivity.equals(r.realActivity)) {

so it’s just checking the launch flags on the intent, and not checking the manifest. compare this to jellybean mr1 release:

look at line 2835 —

                if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
                        || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
                        && taskTop.realActivity.equals(r.realActivity)) {

so it seems that in mr1, they are checking r.launchMode (presumably the AndroidManifest.xml value) in addition to the launchFlags (presumably of the intent itself), whereas in the older versions, they were only checking the intent flags.

it was actually fixed on february 16th, 2012, in this commit:

short version is – set the single top flag in both the intent and in the manifest.

(thanks to this comment on the associated bug in the android bug tracker for pointing me to which source file to look in –

Leave a Reply

Your email address will not be published. Required fields are marked *