简体   繁体   中英

Clicking Android notification creates a new activity instead of going to existing one

My app has a single running activity that creates a notification. When the notification is selected, instead of going to the running activity, the running activity is destroyed and a new activity is created - IN ANDROID 3.0 AND HIGHER. How do I prevent this?

This question has been answered many times, generally pointing out the flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP, and launchMode="singleTop" in the manifest. But most of the answers were before Android 3.0, so is something else needed to fix this in the newer Android versions?

The following simple test app demonstrates my problem:

NotifyTest.java:

package com.example.notifytest;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

public class NotifyTest extends Activity {
    private static int nextCode = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify_test);

        int notifyCode = nextCode++;

        Intent notIntent = new Intent(this, NotifyTest.class);
        notIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        TaskStackBuilder stkBuilder = TaskStackBuilder.create(this);
        stkBuilder.addParentStack(NotifyTest.class);
        stkBuilder.addNextIntent(notIntent);

        NotificationCompat.Builder notBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("NotifyTest")
            .setContentText("notification #" + notifyCode)
            .setContentIntent(stkBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notifyCode, notBuilder.build());
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notifytest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.notifytest.NotifyTest"
            android:label="@string/app_name"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I've verified that this works when emulating every version before 3.0 back to 1.6, and also on a real device running Froyo. When the notification is clicked, onCreate is not called again, so the running activity is selected without making a new notification.

It does not work when emulating every version from 3.0 up, and also on a real device running Jelly Bean. Selecting the notification causes onDestroy and onCreate to be called, and each time you will see a new notification added to the drawer.

PLEASE NOTE: I am strictly interested in preventing restart of the activity - ie the extra calls to onCreate and onDestroy - NOT just the multiple notifications in the drawer. Multiple notifications (caused by multiple calls to onCreate) are just an easy way to demonstrate the actual issue, which is the activity restarting.

Thanks in advance for any help with this!

I had the same problem. My issue was showing up in on API 22/23 emulators/devices and built to a target of API 17 / Version 4.2 builds so that's after 3.0 as you requested Altering the manifest worked very easily for me. It was adding the "singleTask" that made the difference.

    <activity
        android:name=".MainActivity"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM