Stan Malcolm
Stan Malcolm

Reputation: 2790

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?

Upvotes: 9

Views: 6763

Answers (4)

rajesh
rajesh

Reputation: 1

This is what worked for me.

// Parent Fragment
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    getTargetFragment().onActivityResult(requestCode, resultCode, data);
}

// In Child Fragment
public void pick() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);

        // Note followings two lines.
        getParentFragment().startActivityForResult(Intent.createChooser(intent, "Select"), 1);
        getParentFragment().setTargetFragment(this,1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("DEBUG","ONE1");
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
            Log.d("DEBUG","TWO");
            Uri uri = data.getData();
            try {
                Log.d("DEBUG","THREE");
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri);
                // Log.d(TAG, String.valueOf(bitmap));
                Log.d("DEBUG","FOUR");
                ImageView imageView = (ImageView) yourLayoutView.findViewById(R.id.yourImageView);
                Log.d("DEBUG","FIVE");
                imageView.setImageBitmap(bitmap);
                Log.d("DEBUG", "SIX");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}

Upvotes: 0

Claud
Claud

Reputation: 1075

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);
    }
}

Upvotes: 20

Juan Labrador
Juan Labrador

Reputation: 1214

Check out https://stackoverflow.com/a/24303360/3207339

All indicate that Its a problem when is a Fragment inside ViewPager, I have the same problem.

Upvotes: 0

milez
milez

Reputation: 2201

You need to make sure that the activity that you start for results also returns a result. For example in your image capture Activity, where the Activity ends, include

    Intent intent = new Intent();
    //intent.putExtra
    intent.putExtra(getString(R.string.MY_RESULT_CODE), data);
    setResult(RESULT_OK, intent);
    finish(); //Ends the current activity. 
    //After this, onActivityResult of the activity that started this one will be called

Upvotes: 0

Related Questions