Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

android - 片段中未调用onActivityResult(onActivityResult is not being called in Fragment)

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

(相机活动返回时,承载此片段的活动将调用其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.

(但是,永远不会点击onActivityResult 。)

I've set breakpoints, but nothing is triggered.

(我已经设置了断点,但是什么也没有触发。)

Can a fragment have onActivityResult ?

(片段可以具有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);
    }
}
  ask by Spidy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The hosting activity overrides onActivityResult() , but it did not make a call to super.onActivityResult() for unhandled result codes.

(托管活动会覆盖onActivityResult() ,但不会为未处理的结果代码调用super.onActivityResult() 。)

Apparently, even though the fragment is the one making the startActivityForResult() call, the activity gets the first shot at handling the result.

(显然,即使该片段是调用startActivityForResult()的片段,该活动也会在处理结果时获得第一炮。)

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.

(一旦为所有未处理的结果实现了super.onActivityResult() ,该片段就可以处理结果了。)

And also from @siqing answer:

(并且也来自@siqing答案:)

To get the result in your fragment make sure you call startActivityForResult(intent,111);

(要获得片段中的结果,请确保调用startActivityForResult(intent,111);)

instead of getActivity().startActivityForResult(intent,111);

(而不是getActivity().startActivityForResult(intent,111);)

inside your fragment.

(在您的片段中。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...