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
onActivityResult() called prematurely
提问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);
}
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
.
MyConfigure
班级没有任何setResult()
电话。事实上,MyConfigure
除了OnCreate()
使用addPreferencesFromResource
.
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?
如今onActivityResult
被称为具有requestCode
的1458
过早,之后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 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.
Android 中有一个错误/功能 (?),它会立即报告结果(尚未设置)Activity
,声明为singleTask
(尽管活动继续运行)。如果我们launchMode
将父活动从更改singleTask
为singleTop
,则一切都按预期进行 - 结果仅在活动完成后报告。虽然这种行为有一定的解释(只能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_FOREGROUND
flag 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, MyConfigure
class finishes itself and when it happens PreferenceActivity
just assumes that there might be a result from MyConfigure
because 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 MyConfigure
class is forcibly finished.
所以,我认为最好把重点放在为什么你的MyConfigure
课被强行完成上。