这是我的问题 -
我将我的.apk文件复制到手机存储卡上并点击它启动我的应用程序,它允许我安装我的应用程序.我安装我的应用程序.最后,我弹出系统安装包含两个选项"打开"和"完成".当我点击"打开"我的应用程序启动.到目前为止一切正常,没有任何问题.
现在在我的应用程序中,我单击一个按钮,结果发生了一些下载(显示进度对话框).现在我按下一个Home按钮,所以我的应用程序转到后台.
现在我再次启动我的应用程序,进入菜单并单击我的应用程序图标.
预期结果 - 我仍然应该看到下载进度对话框.实际结果 - 我的应用程序的新实例/会话正在开始.
那么如何避免这种情况,以便我的应用程序只能运行一个和一个实例/会话.
@Palejandro,你在这里.将下面的代码放入主活动 onCreate()方法中:
// Possible work around for market launches. See
// http://code.google.com/p/android/issues/detail?id=2373
// for more details. Essentially, the market launches the main activity
// on top of other activities.
// We never want this to happen. Instead, we check if we are the root
// and if not, we finish.
if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
Log.w(TAG, "Main Activity is not the root. Finishing Main Activity instead of launching.");
finish();
return;
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的项目中使用了这段代码,它运行正常!
我相信你需要投入
<activity
android:launchMode="singleInstance"
</activity>
Run Code Online (Sandbox Code Playgroud)
在清单文件中.
| 归档时间: |
|
| 查看次数: |
8339 次 |
| 最近记录: |