android - 单击 Android 通知会创建一个新 Activity ,而不是转到现有 Activity

标签 android android-activity notifications oncreate ondestroy

我的应用有一个创建通知的运行 Activity 。选择通知后,不会转到正在运行的 Activity ,而是会销毁正在运行的 Activity 并创建一个新 Activity - 在 ANDROID 3.0 及更高版本中。我该如何防止这种情况?

这个问题已经回答了很多次了,一般都指出了flag FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_SINGLE_TOP,以及manifest中的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,因此会选择正在运行的 Activity,而不会发出新的通知。

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

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

提前感谢您对此的任何帮助!

最佳答案

我遇到了同样的问题。我的问题出现在 API 22/23 模拟器/设备上,并构建为 API 17/版本 4.2 构建的目标,因此在您请求的 3.0 之后更改 list 对我来说非常容易。它添加了与众不同的“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>

关于android - 单击 Android 通知会创建一个新 Activity ,而不是转到现有 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21318699/

相关文章:

android - XML 选择器背景不适用于 2.3

android - Android BluetoothGattCharacteristic 中 PERMISSION_READ 和 PROPERTY_READ 的区别

android - 阻止用户在退出应用程序后点击 'back'

java - Android - 为什么使用接口(interface)被认为是 Activity 和 Fragment 之间通信的最佳实践?

java - 定位图像以适合所有尺寸的屏幕

android - 登录行为中的 Activity LauchMode

ios - 我可以合并/修改通知栏中的通知吗?

swift - 设备 token 未保存

ruby-on-rails - 在 Rails 中实现通知

Android recyclerview 在 kotlin 中具有不同的 View 类型