Reputation: 2259
I know I can send data to other activities using putExtra()
.
In this case, the receiving intent has to manage the data. Is there a way to send data to another intent and the data is being returned when the intent finishes in onActivityResult()
?
What I want to do:
search for a contact using user input as name
if nothing is found, start contact picker intent
when contact picker returns the contact the user picked, still be able to know the initial input the user made
So I thought about sending the user input to the contact picker intent and making it return this data too.
Any ideas?
Upvotes: 3
Views: 3220
Reputation: 30994
Yes, look at startActivityForResult
and onActivityResult
, where you pass an identifier to startActivityForResult
that identifies the sender e.g.:
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
break;
When the targeted intent has finished the system calls onActivityResult
public void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// code 1 = take picture
if(requestCode==1 && resultCode==RESULT_OK) {
here the '1' from startAct.. is passed in as requestCode
parameter and the results in 'data'. What is actually sent depends on the activity you call.
E.g. in your own code you could do the following in the called Intent to e.g. return the string "Hello World" to the caller ("data" is the key later):
Intent intent = new Intent();
intent.putExtra("data", "Hello World");
setResult(RESULT_OK,intent);
And then retrieve it via data.get("data")
For some more examples have a look at this class for the first part (lines 373+) and this one for an own intent that returns data.
Upvotes: 2