In my previous post, I had explained how to switch (call) to another activities and passing value to it. This time I will try to explain on how to return some value back to the calling activity.
Returning some values to the calling activity can be done by the following example:
Intent resultData = new Intent();
String token = "some token";
String verifier = "some verifier";
resultData.putExtra("token", token);
resultData.putExtra("verifier", verifier);
setResult(Activity.RESULT_OK, resultData);
finish();
This is how it works:
- Create an intent that will hold your data.
- put extra data to the intent by calling putExtra() method to the intent
- call activity’s setResult() method and passing the resultCode and the intent.
- call finish() method to finish the activity and returning to the calling activity.
In calling activity, you need to override the onActivityResult method to catch the returned value from other activity. By assuming that you had previously called the returning value activity by the following example:
final int BROWSER_ACTIVATION_REQUEST = 2; // request code
startActivityForResult(someIntent, BROWSER_ACTIVATION_REQUEST);
requestCode is used to distinguish between different multiple activity calling when getting the activity results.
Then, to get the activity result, you need to simply override the onActivityResult method. For example:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the results is coming from BROWSER_ACTIVATION_REQUEST
if (requestCode == BROWSER_ACTIVATION_REQUEST) {
// check the result code set by the activity
if (resultCode == RESULT_OK) {
// get the intent extras and get the value returned
String token = data.getExtras().getString("token");
String verifier = data.getExtras().getString("verifier");
// do something with returned value
// Tip: check for the null value before you use the returned value,
// otherwise it will throw you a NullPointerException
}
}
}
thanks, this is work for me
thanks
hello Aryo
thanks for sharing this post
my problem(calling activity) is solved
thanks again
Thanks bro
Nice post. Thanks for sharing this useful information.
love,its work for me
thanks for sharing this post
my problem(calling activity) is solved
thanks SO MUCH
THANKS
mantap bermanfaat banget bang
Ok sir
sangar bermanfaat postingan tentang android returningnya
Thanks
H
thanks for sharing tutorial calling activy for android
Nice tutorial it’s dope
NiCe post thanks for sharing this information
Nice post it’s useful and very resourceful keep it up
thnks for share code
akhirnya dapet juga fungsinya..
Wih keren nih
Makasih bang Aryo Pinandito, literaturnya sangat jelas
Nice post bro
Good bro
Android the best
Nice a post thanks
download lagu mp3 gratis hanya di 2019 yang terbaik
Nice post it’s useful and very resourceful
Tanks mas infonya bermanfaat
Literaturnya dapat saya pahami bang makasih banyak
keep
up the good work!
Makasih Mas Saya Jadi Tau
Matursuwun sangat bermanfaat
Your article reflects the issues people care about. The article provides timely information that reflects multi-dimensional views from multiple perspectives. I look forward to reading quality articles containing timely information from you. Thank you for sharing this great information.
Tanks mas infonya bermanfaat
Thank you for sharing this great information
Giá phế liệu inox 304 36.500 – 71.900đ/kg
Giá phế liệu inox 201 20.000 – 40.700đ/kg
Giá phế liệu inox 430, 410, 409 15.500 – 37.000đ/kg
Giá phế liệu inox 510 12.000đ – 16.10đ/kg
Giá phế liệu inox 630 50.100 – 95.100đ/kg
Giá phế liệu inox 205, 253 10.100 – 20.200đ/kg
Giá inox vụn, ba dớ inox 10.200 – 12.200đ/kg
Great job for publishing such a beneficial web site
All your hard work is much appreciated.
Thanks for sharing a great article. You are providing wonderful information, it is very useful to us. Keep posting like this informative articles. Thank you.
I appreciate your post thanks for sharing the information.
This is really great work. Thank you for sharing such a useful information here in the blog.
Very informative and impressive post you have written, this is quite interesting . Great write-up!