How to use createPendingResult to Communicate between service and fragment?


#1

Reading chapter Services And Command Pattern:
In one section of communication between activity and service you mentioned-

Your activity can call createPendingResult(). This returns a PendingIntent. In this case, the PendingIntent will cause a result to be delivered to your activity’s implementation of onActivityResult(), just as if another activity had been called with startActivityForResult() and, in turn, called setResult() to send back a result.

I just can’t get how to do that, can you explain this little more?
This Works fine on Activity but when using on Fragment its not working properly.


#2

I use createPendingResult() in one AlarmManager example. In my case, I am passing it to AlarmManager. In the service case, you would pass it to your service, either as an Intent extra (for startService()) or as a binder method parameter (for bindService()).

Your service can then call send() to supply the Intent and result code to be delivered to onActivityResult() of the activity.

This Works fine on Activity but when using on Fragment its not working properly.

There is no createPendingResult() on Fragment. Your fragment can call createPendingResult() on the Activity, but the result will go to the activity.