onActivityResult不会被调用的片段 [英] onActivityResult not being called in Fragment

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

问题描述

修改

托管该片段的活性有 onActivityResult 调用时相机活动返回

我的片段启动的结果与送相机拍照的意图的活动。精美图片应用程序加载,拍照,并返回。该 onActivityResult 但是永远不会打。我给自己定个破发点,但没有被触发。可一个片段有 onActivityResult ?我想是这样,因为它提供的功能。为什么这个思想不被触发?

  ImageView的MYIMAGE =(ImageView的)inflatedView.findViewById(R.id.image);
myImage.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(视图查看){
        意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
        startActivityForResult(cameraIntent,1888年);
    }
});

@覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == 1888){
        。位图照片=(位图)data.getExtras()获得(数据);
        ((ImageView的)inflatedView.findViewById(R.id.image))setImageBitmap(照片);
    }
}
 

解决方案

所以我想通了这个问题,并且将在对任何人遇到此问题的答案。此次会议的主办活动覆盖了 onActivityResult ,但并没有使未处理的结果codeS呼叫 super.onActivityResult 。显然,即使该片段是一个使 startActivityForResult 呼叫,该活动得到的处理结果的第一枪。这是有道理的,当你考虑片段的模块化。一旦我实现了 super.onActivityResult 所有未处理的结果,该片段得到了射门处理的结果。

和同样来自@siqing答案

要取得结果的片段确保您拨打: startActivityForResult(意向,111); 而不是 getActivity()startActivityForResult(意向,111); 的片段里面

EDIT

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

My fragment starts an activity for 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 break points but nothing is triggered. Can a fragment have onActivityResult? I'd think so since its a provided function. Thoughts on why this isn't 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);
    }
}

解决方案

So I figured out the problem and will post the answer for anyone else experiencing this issue. The hosting activity overrode the onActivityResult but 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.

And also from @siqing answer

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天全站免登陆