Passing values to activities using Extras: Android Programming

preview_player
Показать описание

Passing To an Activity:
-----------------------
Pass data to an activity from MainActivity:
startActivity(intent);

Getting value in second activity's onCreate():
Intent intent = getIntent();
// 0 is the default value if the parameter not set.

Returning a value:
------------------
Change calling code (1st activity) to use startActivityForResult():
startActivityForResult(intent, 12345);
// 12345 is a code to myself to identify which call is returning.

Set data in the called activity (2nd activity):
Intent returnData = new Intent();
setResult(Activity.RESULT_OK, returnData);
// use Activity.RESULT_CANCELED to indicate failure.
finish();

Catch the return value in 1st activity's onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch(requestCode) {
case 12345:
if (resultCode == Activity.RESULT_OK) {

// 0 is default value if none was specified

// Do something with the answer...
break;
}
}
Рекомендации по теме
Комментарии
Автор

Fantastic tutorial, really helped me with passing integers. A lot of tutorials only used strings as examples. Thank you!

Redkaa
Автор

Thank you! Really learned about Extras and as a result, I was able to overcome a big hurdle in my project.

stevennieves
Автор

How is you only have 58 views on this video, because you created a great video, here. Thank you.

bitsinmyblood
Автор

this is so clear and straightforward. THX

ausackermann
Автор

Thank you!  Your videos are great...  more, please, more!

codingwithjoyk
Автор

great tutorial ... paid attention to details ..thanks for sharing 

amitguitarist
Автор

Amazing tutorial, this fixed my code!

alexcombow
Автор

thanks for this tutorial.pls how can save the data in sqlitedatabase and still pass the saved data to another activity?thanks

voiceoftheconscienceint
Автор

How can i pass values without using any button??

shrutichennuri
Автор

How can I pass multiple values to activities? I have 7 different EditTexts that I need to bring into another activity but I cannot do it with this method.

joelowthianYT
Автор

woa really good tutorial saved my life ... but what if everytime I put numbers on the edittext it adds up to the last input in the edittext 
like 
I input a number then press button it shows on the nextactivity then I want to increase the number by pressing backbutton and putting another value on the edittext button then it adds up to the old input not just by changing the value but really adding the new input to the old input. tnx help really appreciated 

altefoersempoi
join shbcf.ru