onActivityResult()被调用在一个意想不到的时间 [英] onActivityResult() is being called at an unexpected time

查看:90
本文介绍了onActivityResult()被调用在一个意想不到的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要活动中,我有:

In my main activity, I have:

    Intent settings_intent = new Intent(this, SettingsActivity.class);
    settings_intent.putExtra("SomeID", some_int);
    startActivityForResult(settings_intent, 1);

,然后在我的SettingsActivity,我有:

And then, in my SettingsActivity, I have:

@Override
public void onBackPressed() {
    super.onBackPressed();
    // pass back settings:
    Intent data = new Intent();
    data.putExtra("SomeThing", some_number);
    setResult(200, data);
    finish();
}

最后,我推翻我的主要活动如下:

And finally, I overrode the following in my Main activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    makeToast("called");
    super.onActivityResult(requestCode, resultCode, data);
}

不过,所谓的敬酒,一旦发生,因为我的设置活动开始,当它完成,而不是。我花了相当长一段时间在这了。任何帮助是AP preciated。谢谢你。

However, the "called" toast happens as soon as my "Settings" activity starts, instead of when it finishes. I've spent quite a while on this now. Any help is appreciated. Thanks.

推荐答案

最有可能设置的活动有它的发射方式在你的清单中设置为 singleTask 。这导致立即取消响应时调用 startActivityForResult()

Most likely your settings activity has it's launch mode set to singleTask in your manifest. This leads to a immediate cancel response when calling startActivityForResult().

请注意,此方法只应与意向协议使用的   被定义为返回一个结果。在其它协议(如   ACTION_MAIN或ACTION_VIEW),你可能不会当你得到的结果   期望。例如,如果活动你的启动使用   singleTask启动模式,它不会在你的任务运行,因此你会   立即收到取消的结果。

Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.

的<一个href="http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29"><$c$c>startActivityForResult()文档

这篇关于onActivityResult()被调用在一个意想不到的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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