如果我从片段的结果开始活动,我应该在片段或活动中调用活动结果吗? [英] If I start activity for result from a fragment, should I call on activity result in the fragment or activity?

查看:87
本文介绍了如果我从片段的结果开始活动,我应该在片段或活动中调用活动结果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定什么是正确的方法.我呼吁采取行动,导致产生不同的碎片.每个片段都应该有自己的onActivityResult吗?还是全部由活动的onActivityResult处理?

I am not sure what would be the correct way. I have a few calls for action for result in different fragments. Should each fragment have its own onActivityResult? Or should it all be handles in the activity's onActivityResult?

推荐答案

您必须为通过startActivityForResult启动活动的每个Fragment实现onActityResult. 因此,每个片段都可以正确跟踪结果:活动结果仅对请求它的片段有意义.

You must implement the onActityResult for every Fragment which started an activity via startActivityForResult. So, each fragment can track the result properly: The activity result makes sense only for the fragment which requested it.

还有一个原因.

开始活动时,必须设置requestCode:

When you start an activity, you have to set a requestCode:

// Note the request code: 0
startActivityForResult(0, new Intent(...))

但是,如果您从Fragment内部调用该方法,则Android会在内部更改请求代码(以便它可以自动跟踪触发请求的片段). 发生这种情况是因为Fragment的主机是FragmentActivity而不是简单的Activity

However, if you call that method from inside a Fragment, Android will internally change the request code (so it can automatically track the fragment which triggered the request). This happens because the host of a Fragment is a FragmentActivity and not a simple Activity

// From FragmentActivity
// Note how the request Code is overriden ((requestIndex + 1) << 16) + (requestCode & 0xffff)
startActivityFromFragment(...) {
    startActivityForResult(this, intent, ((requestIndex + 1) << 16) + (requestCode & 0xffff), options);
}

因此,当触发onActivityResult时,您可以在主机活动中捕获结果.但是,在主机活动中,requestCode不再是您发送的那个,而是由Android内部在内部更改了requestCode:

So, when the onActivityResult is triggered, you can capture the result on the host activity. However, in the host activity, the requestCode is no longer the one that you sent but requestCode changed by Android internally:

In host activity:
@Override
protected void onActivityResult(final int requestCode, final int resultCode,
                                @Nullable final Intent data) {
    // This print 65536
    Log.v("TEST", "onActivityResult: " + requestCode);
    super.onActivityResult(requestCode, resultCode, data);
}

如您所见,您可以在主机"活动上捕获活动结果.但是,请求代码不再是您设置的代码(0).因此,在这里,您无法跟踪请求代码.因此,您无法跟踪此结果的来源.

As you can see, you can capture the activity result on the Host activity. However, the request code is not longer that one you set (0). So, here, you can't track the request code. So, you can't track from who this result is from.

Android将在您的片段中调用onActivityResult.但是,在调用片段之前,请求代码将转换回您发送的值(0):

Android will invoke onActivityResult in your fragment. However, before invoking the fragment, the request code is converted back to the value that you sent (0):

// From FragmentActivity
// Note how the request Code is converted back to 0
onActivityResult(....) {
    // Android will call your fragment with the correct requestCode
    targetFragment.onActivityResult(requestCode & 0xffff, resultCode, data);
}

//In your fragment:
@Override
protected void onActivityResult(final int requestCode, final int resultCode,
                                @Nullable final Intent data) {
    // This print 0. So, now, you know this is the result of the request 0
    Log.v("TEST", "onActivityResult: " + requestCode);
}

因此,还有一个理由在您的片段中实现onActivityResult.特别是,如果您有不同的片段开始不同的活动等.如果您总是开始相同的活动,您可能会错误地认为可以在任意位置实现onActityResult.但是,事实并非如此. 每个片段或活动都应处理他们请求的活动结果.他们不应该处理其他实体的结果.您可以这样做,但只会增加不必要的代码复杂性.

So, there's also a reason to implement the onActivityResult in your fragment. Specially if you have different fragments starting different activities etc. If you always start same activity, you may wrongly assume you can implement the onActityResult wherever you want. However, that is not true. Every fragment or activity should handle the activity result they requested. They should not handle the result of other entitities. You can do that but it will only add unnecessary complexity to your code.

这篇关于如果我从片段的结果开始活动,我应该在片段或活动中调用活动结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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