单击Android通知会创建一个新活动,而不是转到现有活动

我的应用程序有一个运行活动,可以创建通知.选择通知后,不会转到正在运行的活动,而是销毁正在运行的活动并创建新活动 – IN ANDROID 3.0 AND HIGHER.我该如何防止这种情况?

这个问题已经多次回答,通常在清单中指出标志FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_SINGLE_TOP,以及launchMode =“singleTop”.但大多数答案都是在Android 3.0之前,那么在新的Android版本中需要解决这个问题吗?

以下简单测试应用程序演示了我的问题:

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>

我已经验证这在将3.0之前的每个版本模拟回1.6时,以及在运行Froyo的真实设备上都有效.单击通知时,不会再次调用onCreate,因此会选择正在运行的活动而不会发出新通知.

在模拟3.0版本的每个版本时,以及在运行Jelly Bean的真实设备上,它都不起作用.选择通知会导致调用onDestroy和onCreate,并且每次您都会看到添加到抽屉的新通知.

请注意:我对防止重新启动活动非常感兴趣 – 即额外调用onCreate和onDestroy – 而不仅仅是抽屉中的多个通知.多次通知(由多次调用onCreate引起)只是演示实际问题的简单方法,即重新启动的活动.

在此先感谢您对此的任何帮助!

最佳答案 我有同样的问题.我的问题出现在API 22/23仿真器/设备上,并构建到API 17 /版本4.2构建的目标,因此在您请求3.0之后,更改清单对我来说非常容易.它正在添加产生差异的“singleTask”.

    <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>
点赞