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

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

问题描述

当相机活动返回时,承载此片段的活动将调用其onActivityResult.

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

我的片段开始了一个结果活动,并发送了相机拍照的意图.图片应用程序可以正常加载,拍照并返回.但是,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);
    }
}

推荐答案

托管活动会覆盖onActivityResult(),但未对未处理的结果代码调用super.onActivityResult().显然,即使片段是进行startActivityForResult()调用的片段,该活动也会在处理结果时获得第一枪.当您考虑片段的模块化时,这很有意义.一旦为所有未处理的结果实现了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.

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

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