简体   繁体   中英

Selecting image from gallery but the result returned to the calling activity but not the fragment

I have a class where i have navigation drawer. Then as a navigation drawer option I have a fragment,in which if we click on an option then another fragment is called replacing the current one. In the new fragment on clicking of a button I want to select an image from the gallery and then display it in the same fragment.

I have successfully got to the part where we select the image from the gallery,but after selecting the image, the app goes to the calling activity.

I am using startActivityForResult and onActivityResult for this.

private void selectImage() {
    final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, REQUEST_CAMERA);
            } else if (items[item].equals("Choose from Library")) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"),
                        SELECT_FILE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            Toast.makeText(this.getActivity().getBaseContext(), "Reached onActivityResult:Camera", Toast.LENGTH_LONG).show();

        } else if (requestCode == SELECT_FILE) {
            Toast.makeText(this.getActivity().getBaseContext(), "Reached onActivityResult:Gallery", Toast.LENGTH_LONG).show();

        }

    }
}

After selecting an option from the dialog and then selecting the image,onActivityResult is getting called, but then the hosting activity of the fragment is called and the view changes and the image does not persist

Any help is appreciated.

TIA

You need to call super.onActivityResult() for all unhandled request codes. In android Activity always gets a first shot at handling the the result via onActivityResult() .

you call startActivityForResult() in the Fragment.

Refer to this answer: onActivityResult is not being called in Fragment

I just tried it and it seems to be working for me. here is the code

Fragment

private void selectImage() {
    final CharSequence[] items = {
            "Take Photo", "Choose from Library", "Cancel"
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, REQUEST_CAMERA);
            } else if (items[item].equals("Choose from Library")) {
                Intent intent =
                        new Intent(Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("onActivtyResult: ","Fragment");
    super.onActivityResult(requestCode, resultCode, data);
}

Activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("onActivtyResult: ", "Activity");
    super.onActivityResult(requestCode, resultCode, data);
}

Logcat

10-07 12:26:38.348  3781  3781 I onActivtyResult: : Activity
10-07 12:26:38.348  3781  3781 I onActivtyResult: : Fragment

Although I'm late, solution might be useful to others The issue is with lifecycle method. Once we call an implicit intent and then return, activity's on resume is called. If you've added code to update fragment in onResume, it'll dismiss your fragment. So change onResume accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM