java 防止重复活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4001441/
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-10-30 04:18:47  来源:igfitidea点击:

Prevent duplicate activities

javaandroid

提问by Vic V

So, I'm both new to Java and to creating android apps, but not new to programming. I've read through most of the developer.android.com site, but I haven't been able to find this:

所以,我对 Java 和创建 android 应用程序都是新手,但对编程并不陌生。我已经阅读了 developer.android.com 网站的大部分内容,但我一直找不到这个:

I want to make sure that a certain activity isn't running more than once at the same time. So we have a task somewhat like this:
Activity A) a TabActivity, which launches
Activity B) a ListView that, on-click, opens up
Activity C) which is the interface for a mediaplayer object

我想确保某个活动不会同时运行多次。所以我们有一个类似这样的任务:
Activity A) 一个 TabActivity,它启动
Activity B) 一个 ListView,点击后打开
Activity C) 它是媒体播放器对象的接口

Right now, whenever somebody presses the back-button whilst in C (Which is a likely thing, because they're going to listen to a streaming 1-hour long mp3) and then presses another list item, instead of returning to C, C is opened a second time, and two streams are playing. Of course, I'd want only one instance of C running, and I want a second click on the list item to bring C back to the front. This could also be useful for notification intents.

现在,每当有人在 C 中按下后退按钮(这很可能,因为他们将收听 1 小时长的 mp3)然后按下另一个列表项,而不是返回 C、C第二次打开,两个流正在播放。当然,我只希望运行一个 C 实例,并且我希望再次单击列表项以将 C 带回到前面。这对于通知意图也很有用。

I've been messing around with the flags (especially FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_REORDER_TO_FRONT), but no success so far.

我一直在搞乱标志(尤其是 FLAG_ACTIVITY_NEW_TASK、FLAG_ACTIVITY_CLEAR_TOP 和 FLAG_ACTIVITY_REORDER_TO_FRONT),但到目前为止没有成功。

If someone could help me out here I could focus on my next challenge - making it a real feed reader :P

如果有人可以在这里帮助我,我可以专注于我的下一个挑战 - 使其成为真正的提要阅读器:P

Thanks in advance,

提前致谢,

回答by Falmarri

You need to flag your actvity as either "singleTask"or "singleInstance"in your manifest. I can't remember the exact differences between the 2, but either should do what you want. SingleInstancejust does something different with the stack.

您需要将您的活动标记为"singleTask""singleInstance"在您的清单中。我不记得两者之间的确切区别,但两者都应该做你想做的。SingleInstance只是对堆栈做了一些不同的事情。

Example :

例子 :

<activity android:name="MainActivity" android:launchMode="singleInstance"></activity>

You can handle new calls to startActivity() from the same activity instance with onNewIntent()

您可以使用相同的活动实例处理对 startActivity() 的新调用 onNewIntent()

回答by Vic V

I've got it!

我懂了!

For those reading this question and wanting to know the summary: I mistakenly thought more then one activity was running, but it appeared more MediaPlayer instances where running. I made my mediaplayer a class member and am now controlling it from the onStart() event.

对于那些阅读这个问题并想知道摘要的人:我错误地认为不止一个活动正在运行,但出现了更多的 MediaPlayer 实例正在运行。我让我的媒体播放器成为班级成员,现在从 onStart() 事件控制它。

I am using SharedPreferences to check if the stream needs to reset and change source, or continue running and just show the interface.

我正在使用 SharedPreferences 检查流是否需要重置和更改源,或者继续运行并仅显示界面。

Thanks for all your reactions. Really helped me out.

感谢您的所有反应。真的帮了我大忙。

回答by chandrsekar.L.S.

Just Edit the package names in .xml

只需编辑 .xml 中的包名称

EX: com.org.MainActivity change to .org.MainActivity

例如:com.org.MainActivity 更改为 .org.MainActivity

It works for me.....yup

它对我有用.....是的