Android onActivityResult() 过早调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3354955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:51:38  来源:igfitidea点击:

onActivityResult() called prematurely

androidandroid-activity

提问by Eugene Mayevski 'Callback

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

我从我的工人活动开始Activity(的后代PreferenceActivity)如下:

@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);
}

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

MyConfigure班级没有任何setResult()电话。事实上,MyConfigure除了OnCreate()使用addPreferencesFromResource.

Now onActivityResultis called with requestCodeof 1458prematurely, right after MyConfigureactivity 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?

如今onActivityResult被称为具有requestCode1458过早,之后MyConfigure活动运行。在 1.6 和 2.1 模拟器以及 2.1 设备上进行了测试。有setResult()埋在某处的电话PreferenceActivity吗?或者如何解释这种过早的调用?

回答by Eugene Mayevski 'Callback

This is fixed by changing the launch mode to singleTop:

这是通过将启动模式更改为singleTop

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

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 launchModeof the parent activity from singleTaskto singleTop, everything works as expected - result is reported only after the activity is finished. While this behavior has certain explanation (only one singleTaskactivity can exist and there can happen multiple waiters for it), this is still a not logical restriction for me.

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

回答by teapeng

I solved my problem after removing intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);before calling fragment.startActivityForResult(intent, 0);.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);在调用之前删除后解决了我的问题fragment.startActivityForResult(intent, 0);

回答by Felipe

I just removed all my custom "android:launchMode" from my Activity and everything worked like a charm. It is not a good idea change this when you don't know EXACTLY what Android is understanding... Android is a little tricky in this way.

我刚刚从我的 Activity 中删除了我所有的自定义“android:launchMode”,一切都像魅力一样。当您不完全了解 Android 所理解的内容时,改变这一点并不是一个好主意……Android 在这种方式上有点棘手。

回答by Sam

This happened to me when the intent had the Intent.FLAG_RECEIVER_FOREGROUNDflag set.

当意图Intent.FLAG_RECEIVER_FOREGROUND设置标志时,这发生在我身上。

(Yes, that flag isn't activity-related, but I had it on all my intents as part of a shotgun solution to a different problem.)

(是的,该标志与活动无关,但作为针对不同问题的霰弹枪解决方案的一部分,我的所有意图都包含它。)

回答by optimystery

Again as in Mayra's comment, setResult()has nothing to do with your problem. for some reason, MyConfigureclass finishes itself and when it happens PreferenceActivityjust assumes that there might be a result from MyConfigurebecause that's how you wrote the code.

同样在 Mayra 的评论中,setResult()与您的问题无关。出于某种原因,MyConfigure类会自行完成,当它发生时PreferenceActivity只是假设可能会有结果,MyConfigure因为这就是您编写代码的方式。

this also happens when you force back any activity thats started with startActivityForResult()...

当您强制取消以startActivityForResult()...开头的任何活动时也会发生这种情况

So, I think it's better to focus on why your MyConfigureclass is forcibly finished.

所以,我认为最好把重点放在为什么你的MyConfigure课被强行完成上。