我的android应用程序的两个实例正在运行…如何避免这种情况?

前端之家收集整理的这篇文章主要介绍了我的android应用程序的两个实例正在运行…如何避免这种情况?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_0@
这是我的问题 –

>我将我的.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;
    }
}

我在我的项目中使用了这段代码,它运行正常!

原文链接:https://www.f2er.com/android/314096.html

猜你在找的Android相关文章