onActivityResult() 过早调用 [英] onActivityResult() called prematurely

查看:28
本文介绍了onActivityResult() 过早调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的工作程序活动中启动 Activity(PreferenceActivity 的后代),如下所示:

I start the Activity (descendant of PreferenceActivity) from my worker activity as follows:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1458)
        loadInfo();
}

void showSettingsDialog()
{
    startActivityForResult(new Intent().setClass(this, MyConfigure.class), 1458);
}

MyConfigure 类没有任何 setResult() 调用.事实上,MyConfigure 类除了使用 addPreferencesFromResource 加载首选项的 OnCreate() 之外没有任何代码.

MyConfigure class does NOT have any setResult() calls. In fact, MyConfigure class doesn't have any code except OnCreate() where it loads preferences using addPreferencesFromResource.

现在 onActivityResult 过早地使用 1458requestCode 调用,就在 MyConfigure 活动运行之后.在 1.6 和 2.1 模拟器以及 2.1 设备上进行了测试.是否有对 setResult() 的调用埋在 PreferenceActivity 的某处?或者如何解释这种过早的调用?

Now onActivityResult is called with requestCode of 1458 prematurely, right after MyConfigure activity is run. Tested on 1.6 and 2.1 emulators as well as 2.1 device. Is there a call to setResult() buried somewhere in PreferenceActivity? Or how else can this premature call be explained?

推荐答案

这是通过将启动模式更改为 singleTop 来解决的:

This is fixed by changing the launch mode to singleTop:

    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop">

Android 中存在一个错误/功能 (?),它会立即报告 Activity 的结果(尚未设置),声明为 singleTask(尽管事实如此)活动继续运行).如果我们将父 Activity 的 launchModesingleTask 更改为 singleTop,一切都按预期进行——只有在 Activity 完成后才会报告结果.虽然这种行为有一定的解释(只有一个 singleTask 活动可以存在并且可能会发生多个服务员),但这对我来说仍然是不合逻辑的限制.

There's a bug / feature (?) in Android, which immediately reports result (which has not been set yet) for Activity, declared as singleTask (despite the fact that the activity continues to run). If we change launchMode of the parent activity from singleTask to singleTop, everything works as expected - result is reported only after the activity is finished. While this behavior has certain explanation (only one singleTask activity can exist and there can happen multiple waiters for it), this is still a not logical restriction for me.

这篇关于onActivityResult() 过早调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆