How can I use createPendingResult inside a fragment?

I have a user dialog that is created from a fragment. The user can make a choice, and I want that choice to be delivered to the fragment onActivityResult

. For this 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 passed to the parent activity and not the fragment! Difficult for me to call a fragment onActivityResult from parent activity 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 am making inside the fragment:

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

      

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

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if ( requestCode == REQUEST_DIALOG && resultCode == STATUS_CHANGED ) {
        ...
    }       
}

      

+3


source to share





All Articles