通过通知启动活动

通过通知启动活动

本文介绍了通过通知启动活动:避免重复活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我目前呈现出通知。当用户点击这个noticiation,应用程序被启动。通知仍然存在,以指示该服务被在后台运行

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);

不过,我发现其中的错误出现的情况。如果用户开始该应用程序通过点击图标正常,并且该活动运行时点击了通知,则一个新的活动启动时不会发生较早的一个退出的,在前面的顶部的后面的存在。而这还不是全部:在通知进一步点击将创造更多的活动,并把它们放在那些已经运行的顶部。我怎样才能prevent呢?是否有一个很好的检查做,看是否有一定的活动目前正在显示或正在加载的?

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?

推荐答案

这就是它应该是默认的方式。你可能需要指定机器人:launchMode =singleTop如果你想只有一个实例
有4种启动模式,这里的详细信息:http://developer.android.com/intl/de/guide/topics/manifest/activity-element.html

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: http://developer.android.com/intl/de/guide/topics/manifest/activity-element.html

这篇关于通过通知启动活动:避免重复活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:08