0

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

frgr
  • 65
  • 2
  • 11

2 Answers2

0

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
Community
  • 1
  • 1
pgiitu
  • 1,671
  • 1
  • 15
  • 23
  • I am calling super.onActivityResult() in both my fragment as well as the hosting activity – frgr Oct 07 '15 at 18:47
  • can you post some code of `onActivityResult ` in the original post both for Activity and Fragment – pgiitu Oct 07 '15 at 18:59
  • I am not using `getActivity().startActivityForResult()` anywhere. – frgr Oct 07 '15 at 19:22
  • The onActivityResult of the fagment is getting called, but then the hosting activity of the fragment is called and the view changes and the image does not persist.I want to stay on the same fragment from where i am selecting the image. – frgr Oct 07 '15 at 19:29
  • so are you saying that for you `onActivityResult` of the Fragment is getting called first and then `onActivityResult` of Activity? what do u mean by "hosting activity of the fragment is called " ? – pgiitu Oct 07 '15 at 19:32
  • I have an activity(hosting activity) that has the navigation drawer.An option in this calls a fragment.In this fragment, on clicking on a button, another fragment opens from where i call the image picker. Regarding the `onActivityResult` of the Activity, it is never called, i tried debugging using the toast, but it did not reach that part. – frgr Oct 07 '15 at 19:35
  • So you have an Activity `A` . Select an option in Navigation Drawer you have Fragment `F1` in the Activity `A`. Now you tap on a button in `F1` you launch another Fragment `F2` and in the `onCreateView` or somewhere in `F2` you call `selectImage()`. So where do you want to display the image selected by the user in `F1` or `F2`. Also if my understanding is correct why do you have `F2`? – pgiitu Oct 07 '15 at 19:44
  • I want to display the image in F2 itself. – frgr Oct 07 '15 at 19:46
  • I have F2 because of the hierarchical nature of the data that i am collecting from the user. – frgr Oct 07 '15 at 19:47
  • ok. So `onActivityResult` is being called for `F2` and not for the Activity `A`? I tried it with a single fragment and it works. Can you then post code of `F1` where u launch `F2` and also the code in `F2` where u are handling the `onActivityResult` . Also at the end of all this on which fragment did u end up with `F1` ? – pgiitu Oct 07 '15 at 19:53
  • After all this i end up at the activity A. – frgr Oct 07 '15 at 19:58
  • Activity `A` with Fragment `F1` ? You are always in Activity `A`. Do you see the button which once clicked you launch `F2`? – pgiitu Oct 07 '15 at 19:59
  • Hey @pgiitu, I think I have solved the problem.I possibly had some issue with my onResume method in the Activity A.Anyways thanks for discussing.Cheers!!! – frgr Oct 07 '15 at 20:23
  • @frgr good to know that u fixed it. Have a great day !! May be u can explain what were you doing wrong in the onResume so that it is helpful for anyone else doing a similar mistake. – pgiitu Oct 07 '15 at 20:28
  • Actually in onResume method i was calling the Activity A's method that was calling another fragment, so after selecting the image from the picker, when the focus came back to the activity, the onResume method got called that did not save the state of the image selected and gave all together a different view. – frgr Oct 08 '15 at 05:23
0

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.

Ravi Mishra
  • 167
  • 1
  • 7