onActivityResult不会从viewPager片段中调用 [英] onActivityResult doesn't call from viewPager fragment

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

问题描述

我在主片段内使用了带有fragmnets的viewPager. 我正在尝试从图库或照相机中将图像转换为位图,但是在选择照片和startActivityForResult之后,它无法捕获onActivityResult ...

这是我如何调用startActivityForResult:

private void setAvatarDialog(){
        final CharSequence[] options = {"Choose from Gallery", "Take Photo" };

        String title = getString(R.string.alertDialog_editProfile_updateAvatar_title);
        String negative = getString(R.string.alertDialog_editProfile_updateAvatar_negative);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title);
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (options[which].equals(options[0])) {
                    mIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    mIntent.setType("image/*");
                    startActivityForResult(Intent.createChooser(mIntent, "Select File"), SELECT_FILE);
                } else if (options[which].equals(options[1])) {
                    mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(mIntent, REQUEST_CAMERA);
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(negative, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

这是我的onActivityResult:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK){
            if (requestCode == REQUEST_CAMERA){
                resultCamera(data);
            } else if (requestCode == SELECT_FILE) {
                resultGallery(data);
            }
        }
    }

有什么想法吗?

解决方案

当我有一个ParentFragment包含一个ViewPager带有3个不同的Fragments(子项),并且其中一个子项碎片启动了一项活动时,此解决方案对我有用. >

您可以使用getParentFragment().startActivityForResult

在孩子中打开活动

活动完成后,它将在ParentFragment中调用onActivityResult,在这里我覆盖了调用3个孩子的onActivityResult

中的每一个的方法

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (Fragment fragment : getChildFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

Hi I am using viewPager with fragmnets inside main fragment. I am trying to get image to bitmap from gallery or from camera, but after picking photo and startActivityForResult it doesn't catch in onActivityResult...

here is how i call startActivityForResult:

private void setAvatarDialog(){
        final CharSequence[] options = {"Choose from Gallery", "Take Photo" };

        String title = getString(R.string.alertDialog_editProfile_updateAvatar_title);
        String negative = getString(R.string.alertDialog_editProfile_updateAvatar_negative);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title);
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (options[which].equals(options[0])) {
                    mIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    mIntent.setType("image/*");
                    startActivityForResult(Intent.createChooser(mIntent, "Select File"), SELECT_FILE);
                } else if (options[which].equals(options[1])) {
                    mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(mIntent, REQUEST_CAMERA);
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(negative, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

and here is my onActivityResult:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK){
            if (requestCode == REQUEST_CAMERA){
                resultCamera(data);
            } else if (requestCode == SELECT_FILE) {
                resultGallery(data);
            }
        }
    }

any ideas, please?

解决方案

This solution worked for me when I had a ParentFragment that contained a ViewPager with 3 different Fragments (children) and one of the children fragment started an activity.

You can open an activity in a child with getParentFragment().startActivityForResult

Once that activity gets finished it will call onActivityResult in the ParentFragment, where I override the method to call each of the 3 children's onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (Fragment fragment : getChildFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

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

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