Android 通过通知启动活动:避免重复活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2326622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:26:08  来源:igfitidea点击:

Starting Activity through notification: Avoiding duplicate activities

android

提问by pgsandstrom

So I am currently showing a notification. When the user clicks this noticiation, the application is started. The notification persists, to indicate that the service is running in the background.

所以我目前正在显示通知。当用户单击此通知时,应用程序将启动。通知持续存在,表明服务正在后台运行。

Intent notificationIntent = new Intent(context, LaunchActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);

However, I have detected a case where a bug appears. If the user starts the application through clicking the normal icon, and while the activity is running clicks the notification, then a new activity is started without the earlier one exiting, the later being on top of the earlier. And that is not all: Further clicks on the notification will create additional activities and place them on top of those already running. How can I prevent this? Is there a nice check to do to see if a certain activity is currently being shown or is loaded?

但是,我检测到出现错误的情况。如果用户通过单击普通图标启动应用程序,并且在 Activity 运行时单击通知,则会启动一个新的 Activity,而前一个不退出,后一个在前一个的顶部。这还不是全部:进一步点击通知将创建额外的活动并将它们放在已经运行的活动之上。我怎样才能防止这种情况?是否有一个很好的检查可以查看当前是否正在显示或加载某个活动?

回答by yanchenko

That's the way it's supposed to be by default. You probably need to specify android:launchMode="singleTop"if you want to have a single instance only.
There are 4 launch modes, more info here: https://developer.android.com/guide/topics/manifest/activity-element.html

默认情况下应该是这样。您可能需要指定android:launchMode="singleTop"是否只想拥有一个实例。
有 4 种启动模式,更多信息在这里:https: //developer.android.com/guide/topics/manifest/activity-element.html

回答by Thomas Besnehard

When using the lanchMode="singleTask", if an instance of your activity already exists, Android does not re-create the Activity but launch it with the onNewIntent()method.

使用 时lanchMode="singleTask",如果您的 Activity 实例已经存在,Android 不会重新创建 Activity,而是使用onNewIntent()方法启动它。

As documented by Android :

正如 Android 记录的那样:

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

系统在新任务的根创建活动并将意图路由到它。但是,如果 Activity 的实例已经存在,系统会通过调用其 onNewIntent() 方法将意图路由到现有实例,而不是创建一个新实例。

Android documentation for activity mode

活动模式的 Android 文档

回答by TheIT

As the two answers above have mentioned, you'll want to set the application's launch mode which is defined in the activity's definition in the manifest:

正如上面的两个答案所提到的,您需要设置应用程序的启动模式,该模式在清单中的活动定义中定义:

<activity
    android:name="com.company.ActivityName"
    android:launchMode="singleTask">
</activity>

Additionally, you may want to note, that despite FLAG_ACTIVITY_SINGLE_TOP being a valid Intent flag, there are no equivalent intent flags for singleTask or singleInstance.

此外,您可能需要注意,尽管 FLAG_ACTIVITY_SINGLE_TOP 是有效的 Intent 标志,但对于 singleTask 或 singleInstance 没有等效的意图标志。

See the launchMode section for more details on the different launch mode options: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

有关不同启动模式选项的更多详细信息,请参阅 launchMode 部分:http: //developer.android.com/guide/topics/manifest/activity-element.html#lmode