本文介绍了当 Activity 从 Fragment 启动时未调用 onActivityResult()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Android 中从相册导入图片时遇到问题,因为从不调用 onActivityResult()
方法.
I have an issue with importing a picture from the Album in Android, because the onActivityResult()
method is never called.
这是我写的代码(从片段而不是活动调用):
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()
但它从未被触发:
And by the way, I have defined the onActivityResult()
but it's never triggered:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult"); // not printed
}
知道这有什么问题吗?
谢谢!
推荐答案
要在片段中调用 onActivityResult()
,您应该调用片段版本的 startActivityForResult()
,不是活动的.所以在你的片段代码中,替换
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);
这篇关于当 Activity 从 Fragment 启动时未调用 onActivityResult()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!