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
android fragment onactivityresult


source to share


No one has answered this question yet

Check out similar questions:

3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
3
fragments startActivityForResult always return resultCode 0 and intent null on callback onActivityResult
2
Intent issue, return value (Android)
1
super.onActivityResult (requestCode, resultCode, data);
1
Image cropping does not work in Android version 7.0 and later
0
Can't Play Spotify Song
0
Can open the camera, but cannot display other activity
-1
Android: viewing multiple images
-1
Sending one SMS to multiple phone numbers
-2
when the mesg box comes out it goes back to prevoius activyty



All Articles
Loading...
X
Show
Funny
Dev
Pics