android - 使用通知恢复单个实例 Activity

标签 android android-activity android-notifications launchmode

我有一个正常的 Activity ,我们称之为 A,它是打开应用程序时呈现给用户的第一个 Activity 。 Activity A 有一个启动 Activity 的按钮,我们称它为 B, list 中的 launchMode 设置为 singleInstance。 Activity B 做一些处理。

用户可以按下主页按钮并再次打开应用程序,这将向他们展示开始 Activity ,也就是 Activity A。如果用户再次单击该按钮(在 Activity A 中),他们将看到 Activity B。在在这种情况下, Activity B 不会重新启动,即 onCreate 不会被调用,因为它是 singleInstance,这正是我想要的。

我想让用户在按下主页按钮后更容易返回 Activity B。因此,我创建了一个持续通知,让用户恢复 Activity B。一旦 Activity B 完成,正在进行的通知将被取消。

现在问题来了,点击正在进行的通知会再次重新创建 Activity B,即再次调用 onCreate。我不知道为什么这里的行为与单击 Activity A 上的按钮不同。以下是我创建通知的方式:

Intent notifyIntent = new Intent(mContext, CallActivity.class);

PendingIntent notifyPendingIntent = PendingIntent.getActivity(
        mContext, 0, notifyIntent, PendingIntent.FLAG_NO_CREATE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
        .setSmallIcon(R.drawable.baseline_call_white_18)
        .setContentTitle(title)
        .setContentText(text)
        .setOngoing(true)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(notifyPendingIntent);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
notificationManager.notify(NOTIFICATION_ID, builder.build());

谁能告诉我为什么这不起作用,我怎样才能让它按照我描述的方式工作?

编辑

这是我的 list 的 fragment :

<application
    android:name="com.mnm.caller.IMApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
    <activity
        android:name="com.mnm.caller.activities.SplashActivity"
        android:configChanges="orientation|keyboardHidden"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.Splash">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>


    <activity
        android:name="com.mnm.caller.activities.LoginActivity"
        android:configChanges="orientation|keyboardHidden"
        android:theme="@style/AppTheme.NoTitleBar"
        android:screenOrientation="portrait" />

    <activity
        android:name="com.mnm.caller.activities.HomeActivity"
        android:configChanges="orientation|keyboardHidden"
        android:theme="@style/AppTheme.NoTitleBar"
        android:screenOrientation="portrait" />

    <activity
        android:name="com.mnm.caller.activities.CallActivity"
        android:configChanges="orientation|keyboardHidden"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoTitleBar" />


    <service
        android:name="com.mnm.caller.services.IMFirebaseMessagingService"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

</application>

最佳答案

在您写的评论中:

I'm actually developing a calling app. Activity A is the home screen, while B is the calling activity. I want to let the users navigate back to the home screen during a call, so I intentionally and knowingly chose to create two instances of the app in recent apps (you can see the exact behavior in the default phone app on Android)

如果是这种情况,您可能确实希望有 2 个可以在它们之间切换的独立任务。为此,您需要使用 taskAffinity。你的 CallActivity 应该有 singleTasksingleInstance 的启动模式,你应该设置 android:taskAffinity="call" 或类似的东西。为了不混淆您的用户,您还应该为 CallActivity 提供一个不同的 android:label 和一个不同的 android:icon ,这样当这个任务出现在最近的任务列表中,它看起来与应用程序的其余部分不同。确保在启动 CallActivity 时还设置了 FLAG_ACTIVITY_NEW_TASK。您还应该在构建 Notification 时设置此标志。

关于android - 使用通知恢复单个实例 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55092109/

相关文章:

android通知点击不工作

java - Android - 从静态方法制作Toast

java - 如果警报日期等于当前日期,我想创建通知,并且通知将创建从警报日期到疫苗日期的所有日期

android - Cardboard SDK 将 VRVideoView 初始化为全屏和 VR 模式(分屏)

java - Android Auto Exposure Lock 空指针异常

android - 将库源添加到 Android Studio

android - 如何为不同的 Android Activity 分配视觉上不同的启动图标

java - 如何在 onDestroy 函数中取消通知

带有图像的 Android 自定义选项卡选择指示器

android - setAdapter() 上的空指针异常