[英]I dont want more than one instance of my activity
My service has a notification with an intent to launch the associated activity, but I find sometimes that when pressing back in my activity it returns to a previously loaded activity, so it looks like there are sometimes where there are more than one copy of the activity in memory. 我的服务有一个意图启动相关活动的通知,但我发现有时候在我的活动中按下它会返回到之前加载的活动,所以看起来有时会有多个活动副本在记忆中。 How can I avoid this?
我怎么能避免这个? I suppose I must bring to front in case exist or launch a new one but dont know exactly how.
我想我必须带到前面以防万一存在或推出一个新的,但不知道具体如何。 thanks.
谢谢。
In you AndroidManifest.xml
add android:launchMode="singleTask"
with the as attribute with the desire activity you want to run only 1 instance. 在你的
AndroidManifest.xml
添加android:launchMode="singleTask"
,其as属性与你想要运行的欲望活动只有1个实例。
eg 例如
<activity
android:name=".MyActivity"
android:launchMode="singleTask">
You can even use Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
flag with you activity intent. 您甚至可以将
Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
标志与您的活动意图一起使用。 So that when activity is already running it will be brought to front instead of creating new. 因此,当活动已经运行时,它将被带到前面而不是创建新的。
将标志Intent.FLAG_ACTIVITY_CLEAR_TOP添加到您的意图中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.