Reputation: 16278
I'm aware you're supposed to create a PendingIntent
calling Activity#createPendingResult()
method, but the documentation is limited enough not to explain how do you trigger Activity#onActivityResult()
from it. How do I trigger it?
Upvotes: 2
Views: 2284
Reputation: 3000
I'm aware you're supposed to create a PendingIntent callingActivity#createPendingResult() method...
There are different ways to create PendingIntents, and which method you use will depend upon your goal. The quoted phrase makes it sound like OP does not know that. I am going to assume instead that this is a fairly advanced technical question and answer it correctly:
In the component that would like to return a result, you call
pendingResult.send(...)
picking one of the versions of the send method that allows you to specify a result code and a "fill-in" intent. For example,
public void send (Context context, int code, Intent intent)
Some experimenting may be required, but the basic idea is that when you want to "redeem" a pending intent, you use the send()
method.
Upvotes: 1
Reputation: 10278
PendingIntents and onActivityResult are not related except by the method you describe: createPendingResult.
PendingIntents are used to allow another app (or the Android OS) to hold onto a reference to starting an activity in your app.
onActivityResult is used with the startActivityForResult() method. When one activity (ActivityA) starts another (ActivityB) in your app, you can have ActivityA wait for a response from ActivityB. These events occur while the user is interacting with your app.
So for contrast, generally PendingIntents are used to have another app signal your app, while onActivityResult is used when an activity within your app is waiting for the result from another activity. If your activity is expecting a result from another app with a behavior similar to a user interacting with with your app, then you use createPendingResult
- and it will return to your activity and be processed in a similar manner as if it were a part of your app.
The key here is that another activity may not do any processing that is useful for your app. Additionally, you should be mindful that Android may destroy your app (due to limited memory) and then recreate it to process the result. So the process may lead to unexpected or unpredictable results unless you are careful in how you process this type of PendingIntent.
Upvotes: 2