0

So, I've read some threads about this, but I can't get this to work. Basically I have this Dialog in which the user chooses to take a new pic or select a pic from their gallery. Here is the code:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setCancelable(true)
    .setItems(R.array.galeria_camera_array, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int userChoice) {
            if (userChoice == 1) {
                // take photo
            }
            if (userChoice == 0) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, 1);
            }
        }
    });

    return builder.create();
}

And then, the onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //choose file from gallery
}

Can anyone help? Before I have to re-read 10 pages worth of theory again... I'm quite new to this kind of things (onResult). Thank you.

Fábio Santos
  • 199
  • 1
  • 2
  • 16
  • What exactly is happening, and how is this different from what you expect/intend? – frumious Jul 06 '15 at 20:14
  • @frumious the onActivityResult doesn't get called. – Fábio Santos Jul 06 '15 at 20:28
  • http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment – Christine Jul 07 '15 at 00:01
  • It's not clear which `onActivityResult()` you are showing and intending to handle the result in. Both `DialogFragment` and `Activity` have that method. `onActivityResult()` in the `DiaglogFragment` will never be called because the fragment detaches from the activity after the user makes a selection. You need to handle the result in the containing activity. – Bob Snyder Jul 07 '15 at 00:23

3 Answers3

0

The easy way to fix this is to move your fragment's onActivityResult into your activity, or you can use your activity's onActivityResult to call a method in your fragment.

I looked at my code and apparently I managed to get around this by calling this.getActivityForResult from my fragment, and then the result returned to my fragment, but I'll be honest with you: I don't remember writing this.

TBridges42
  • 1,849
  • 1
  • 19
  • 29
0

Try implementing something like this in the activity where the dialog fragment is fired from:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.myFragmentId);
        fragment.onActivityResult(requestCode, resultCode, data);

The underlining activity is what will receive onActivityResult, so u should be able to 'point' it towards onActivityResult of the dialog fragment.

Mark
  • 9,604
  • 5
  • 36
  • 64
  • No need for that. onActivityResult should work in a fragment. You do have to call through to super.onActivityResult, in the fragment, and the activity should not catch the onActivityResult before the fragment does. – Christine Jul 07 '15 at 00:00
  • I've had mixed results with this, some cases it's caught by the fragment and others its caught by the activity (depending on the context of startActivityForResult). This has been a solution for me in the past, and thought it was worth trying since a solution has not yet been found. – Mark Jul 07 '15 at 00:22
0

Ok, these two things made it work:

  1. getActivity().startActivityForResult(galleryIntent, 1); You have to add getActivity()
  2. As you guys said, you have to declare the onActivityResult() in the Activity itself.

Thanks guys!

Fábio Santos
  • 199
  • 1
  • 2
  • 16