防止我的 Android 应用程序的多个实例由单个活动组成 [英] Prevent multiple instances of my Android application composed of a single activity
问题描述
我有一个由单个 Activity
组成的 Android 应用程序.我如何确保在给定时间内只存在我的应用程序的一个实例(== Activity
)?我遇到了一种情况,我通过多次单击应用程序图标成功打开了我的应用程序的多个实例(这不会一直重现).
I have an Android application which is composed from a single Activity
.
How can I assure that only one instance of my application (== Activity
) will exist in a given time?
I got to a situation in which I succeeded to open multiple instances of my app by clicking on the app icon multiple times (this doesn't reproduce all the time).
推荐答案
像这样改变你的清单:
<activity
android:name="com.yourpackage.YourActivity"
android:label="@string/app_name"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
包含 android:launchMode="singleTask"
并且不可能同时启动多个 Activity 实例.查看活动文档了解更多信息.
include the android:launchMode="singleTask"
and it will not be possible to have multiple instances of your Activity launched at the same time. See the activity docs for more.
这篇关于防止我的 Android 应用程序的多个实例由单个活动组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!