onActivityResult doesn't call from viewPager fragment
Asked Answered
D

4

9

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?

Dirham answered 22/8, 2015 at 12:57 Comment(2)
Have you made sure your activity returns a result? Code not shown..Tremayne
you mean that I need to overwrite onActivity result in activity?Dirham
P
20

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);
    }
}
Poohpooh answered 24/11, 2015 at 21:21 Comment(0)
T
0

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
Tremayne answered 23/8, 2015 at 6:58 Comment(1)
I am using this tutorial to get image from gallery and from camera. theappguruz.com/blog/… when i am using it inside one fragment - it works, but when there are is viewPager with fragments it's not workingDirham
A
0

Check out https://mcmap.net/q/53228/-onactivityresult-is-not-being-called-in-fragment

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

Adigun answered 2/10, 2015 at 10:15 Comment(0)
I
0

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();
            }
        }
}
Iinden answered 9/6, 2016 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.