android - 在onResume中调用intent会导致无限递归

标签 android android-intent

每次我离开我的应用程序时,我都需要调用另一个应用程序的 Intent 。这意味着每次我(重新启动)我的 MainActivity 时,我都需要在 onResume 中调用此 Intent :

Intent intent = new Intent();
intent.setData(Uri.parse("protocol://mycall.stuff"));
intent.setAction("protocol.GET");
startActivityForResult(intent, 1);

问题是这个 Intent 导致 onActivityResult 似乎在完成后再次调用 onResume 等等。

我尝试在 onActivityResult 之后使用 finish(),就像其他一些帖子中建议的那样,但随后我的 Activity - 好吧 - 完成。

有什么想法吗?

谢谢!

最佳答案

使用 bool 值来处理这种情况,如下所示...

boolean done = false;

@Override
protected void onResume() {

    super.onResume();

    if (!done) {

        Intent intent = new Intent();
        intent.setData(Uri.parse("protocol://mycall.stuff"));
        intent.setAction("protocol.GET");
        startActivityForResult(intent, 1);

        done = true;

    }

}

关于android - 在onResume中调用intent会导致无限递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22355802/

相关文章:

android - 错误 : Could not access the Package Manager. 系统是否正在运行? - 在安卓工作室

java - 如何修复损坏的导入?

java - 使用存储访问框架打开特定路径

安卓 ListView : Display Item details onItemClic

android - 如何从我的应用程序向 WhatsApp 分享文本?

java - 我正在使用自定义列表有两个按钮,一个表单和第二个用于 whatsapp

android - import"existing projects into workspace"和 "exsisiting android code into workspace"有什么区别?

Android:新的 Google 客户端 API 位置

android - 导航组件 - 如何仅清除嵌套图的历史记录?

java - Android:如何从蓝牙设备控制音乐服务播放/暂停?