简体   繁体   English

如何防止Android应用程序多次加载?

[英]How to prevent Android application from loading multiple times?

This must be an easy one but I'm not having much luck searching for an answer. 这一定是一件容易的事,但我没有太多运气来寻找答案。 Apologies if this is a regular question. 抱歉,这是一个常规问题。

If I navigate away from my app I cannot return to it. 如果我离开我的应用程序,将无法返回。 Starting the app again will load a second instance of it rather than returning to it. 再次启动应用程序将加载它的第二个实例,而不是返回到它。 If I leave an audio loop running in my app, It's hard to get back in and turn it off. 如果我在应用程序中运行音频循环,则很难重新将其关闭。

On startup I'd like the app to destroy any previous instance of itself left running. 在启动时,我希望该应用程序销毁其自身未运行的任何先前实例。

I'd also like to try having the app shut itself down when I navigate away (I know it's not the right way to do things but I'd like to try this for my own personal use of the app). 我还想尝试在离开时关闭该应用程序(我知道这不是正确的操作方式,但我想尝试此操作供我个人使用该应用程序)。 Or have the "back" button destroy the app. 或使用“返回”按钮销毁该应用程序。

Thanks. 谢谢。

Add this to your activity definition in manifest... 将其添加到清单中的活动定义中...

android:launchMode = "singleInstance" android:launchMode =“ singleInstance”

How to prevent the activity from loading twice on pressing the button 如何防止活动在按下按钮时加载两次

我已经回答了这个问题 ,您可以在AndroidManifest.xml文件中为MainActivity声明android:launchMode="singleTask"属性,这样,如果在后台运行一个实例,则操作系统将不会创建新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM