在 Fragment 中未调用 onActivityResult [英] onActivityResult is not being called in Fragment

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

问题描述

托管此片段的活动在相机活动返回时调用其 onActivityResult.

The activity hosting this fragment has its onActivityResult called when the camera activity returns.

我的 Fragment 开始一个 Activity 以获取结果,并发送给相机拍照的意图.图片应用程序加载正常,拍照,然后返回.onActivityResult 然而永远不会被命中.我设置了断点,但没有触发任何内容.片段可以有 onActivityResult 吗?我会这么认为,因为它是一个提供的功能.为什么没有触发?

My fragment starts an activity for a result with the intent sent for the camera to take a picture. The picture application loads fine, takes a picture, and returns. The onActivityResult however is never hit. I've set breakpoints, but nothing is triggered. Can a fragment have onActivityResult? I'd think so since it's a provided function. Why isn't this being triggered?

ImageView myImage = (ImageView)inflatedView.findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, 1888);
    }
});

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if( requestCode == 1888 ) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        ((ImageView)inflatedView.findViewById(R.id.image)).setImageBitmap(photo);
    }
}

推荐答案

宿主 Activity 覆盖了 onActivityResult(),但它没有调用 super.onActivityResult()code> 用于未处理的结果代码.显然,即使片段是进行 startActivityForResult() 调用的片段,Activity 也将首先处理结果.当您考虑片段的模块化时,这是有道理的.一旦我为所有未处理的结果实现了 super.onActivityResult(),片段就可以处理结果了.

The hosting activity overrides onActivityResult(), but it did not make a call to super.onActivityResult() for unhandled result codes. Apparently, even though the fragment is the one making the startActivityForResult() call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once I implemented super.onActivityResult() for all unhandled results, the fragment got a shot at handling the result.

还有来自@siqing 的回答:

And also from @siqing answer:

要在您的片段中获取结果,请确保在您的片段中调用 startActivityForResult(intent,111); 而不是 getActivity().startActivityForResult(intent,111);片段.

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity().startActivityForResult(intent,111); inside your fragment.

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

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