的onActivityResult()时,从活动开始片段不叫 [英] onActivityResult() not called when Activity started from Fragment

查看:141
本文介绍了的onActivityResult()时,从活动开始片段不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从相册中导入Android的图片的问题,因为的onActivityResult()方法不会被调用。

I have an issue with importing a picture from the Album in Android, because the onActivityResult() method is never called.

这是code,我写(从片段而不是所谓的活动):

This is the code that I wrote (called from a fragment not an activity):

Intent galleryIntent = new Intent(Intent.ACTION_PICK,  android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
getActivity().startActivityForResult(galleryIntent, PICK_IMAGE);

顺便说一下,我已经定义了的onActivityResult(),但它从来没有触发:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult"); // not printed
}

任何想法有什么错呢?

Any idea what's wrong with this?

谢谢!

推荐答案

要具有的onActivityResult()堪称片段,你应该叫<$ C的片段的版本$ C> startActivityForResult(),而不是活动的。因此,在您片段的code,替换

To have onActivityResult() called in the fragment, you should call the fragment's version of startActivityForResult(), not the activity's. So in your fragment's code, replace

getActivity().startActivityForResult(galleryIntent, PICK_IMAGE);

startActivityForResult(galleryIntent, PICK_IMAGE);

这篇关于的onActivityResult()时,从活动开始片段不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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