gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:17:21 26 4
gpt4 key购买 nike

我有一个正常的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com