Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult doesn't call from viewPager fragment

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?

like image 699
Stan Malcolm Avatar asked Aug 22 '15 12:08

Stan Malcolm


1 Answers

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);
    }
}
like image 80
Claud Avatar answered Nov 17 '22 13:11

Claud