微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

这是我的问题 –

>我将我的.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.jb51.cc/android/314096.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐