简体   繁体   English

我不想要一个以上的活动实例

[英]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.

相关问题 由于意图而导致的一个以上活动实例 - More than one instance of activity due to intent 如何从堆栈中相距一个以上的另一个活动返回我的主要活动? - How can I return to my main activity from another activity that is more than one away in the stack? 如何为多个活动恢复屏幕活动? - How do I resume screen activity for more than one activity? 如何在一项活动中拥有多个 navHostFragment? - How can I have more than one navHostFragment in one activity? 一项以上的主要活动 - More than one principal activity 如果要在多个活动中添加选项卡,是否需要多个Pager(viewpager)类? - Do we need more than one Pager(viewpager) class, if we want to add tabs in more than one activity? 如何在活动中使用多个广播? - How can I use more than one Broadcast in activity? 我可以在一个活动android中使用2个以上的回收站视图吗? - Can i use more than 2 recycler views in one activity android 我可以在Android编程中为一个活动注册多个BroadcastReceiver吗? - Can I register more than one BroadcastReceiver for an Activity in an Android Programming? 我想将多个sqlite记录发送到服务器 - I want to send more than one record of sqlite to server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM