How can I use createPendingResult inside fragment?
Asked Answered
I

0

7

I have a user dialog which is created from a fragment. The user can make a choice and I want this choice to be delivered to fragment's onActivityResult. To do so, I use createPendingResult. This method works fine for an activity, but for a fragment I need to call getActivity().createPendingResult. This causes the result to be delivered to parent activity instead of fragment! It is kind of complicated for me to call fragment's onActivityResult from parent activity's onActivityResult. Is there another way?

This is the code for the dialog:

public static void selectChoice(final Context c, final PendingIntent callback) {
    (new AlertDialog.Builder(c))
         .setTitle(title)
         .setSingleChoiceItems(items, index, new DialogInterface.OnClickListener() {

         @Override
         public void onClick(DialogInterface dialog, int which) {
             try {
                 callback.send(c, STATUS_CHANGED, 
                               new Intent().putExtra(EXTRA_CHOICE, which));
             } catch (CanceledException e) {
                 e.printStackTrace();
             }                   
             dialog.dismiss();
         }
    }).show();
}

This is the call I make inside the fragment:

selectChoice(getActivity(), getActivity().createPendingResult(
    REQUEST_DIALOG, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT)
);

This is fired in the parent activity, but not in fragment:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if ( requestCode == REQUEST_DIALOG && resultCode == STATUS_CHANGED ) {
        ...
    }       
}
Impel answered 12/11, 2014 at 15:58 Comment(2)
Provide code for clarity, please.Pyre
Please, post some code.Ephialtes

© 2022 - 2024 — McMap. All rights reserved.