Passing data or parameter to another Activity Android
Sometime we need to pass data or parameter to another Activity on Android. Only one activity is active at once. An activity open new activity for result and opened activity need parameter to set their interface or another option based on request. So it is important a system can handle sending and retrieve parameter between two Activity.
To open an activity and wait for a result, we can use this sintax
Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
startActivityForResult(newIntent, 0);
ActivityClass2 is the class name of activity that we need to create and then open it from current activity. startActivityForResult is a method to run newActivity. Later we can have the result by add this function on current Activity
protected void onActivityResult(int requestCode, int resultCode, Intent data)
First to send a data or parameter to newActivity we can use this sintax
Bundle bundle = new Bundle();
bundle.putString(“param1”, “test”);
Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
On ActivityClass2, we can read this parameter using this sintax
Bundle bundle = this.getIntent().getExtras();
String param1 = bundle.getString(“param1”);
be carefull to use param1, because it can be null if we didn’t set it.
So it is very easy to send or passing data or parameter between two activity in Android. One last thing is to return a value for startActivityForResult, we can add this line code to close ActivityClass2 and return focus to opener plus send data to opener activity.
Bundle bundle = new Bundle();
bundle.putString(“status”, “OK”);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
Thanks alot for sharing your knowledge…….
Allah Bless U.
Comment by awad — August 9, 2009 @ 2:27 am
Thank you! It was very useful and exactly what I needed! ;)
Comment by Patricia Pérez — April 15, 2010 @ 4:32 am
thanx for this tut….this is very-very helpful to me
Comment by Dixit — May 31, 2010 @ 5:34 am
Thanks a lot… it really helped me..
say Activity1 starts Activity2 and Activity2 wants to send data to Activity1 (i.e the calling activity)
then we need to override a method in Activity1
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
}
Comment by Amith GC — June 17, 2010 @ 2:48 am
It helps me lot, but can we send List user definded objects, If possible please help
me.
Thank You
(Vikram Kadam)
Comment by Vikram kadam — July 15, 2010 @ 7:34 am
Thanks a lot ! now i can pass the values from 1 activity to other.
Comment by vijay — August 18, 2010 @ 5:18 am
The sample code is really helpful. Thanks a lot :)
Comment by Sayani — September 22, 2010 @ 6:42 am
Thanks a lot, but for instances of other classes how do we do it ? There is not a putObject() override…
Comment by Marcos — October 8, 2010 @ 6:53 am
Thanks buddy for your excellent and simple code
Comment by Jaz — October 13, 2010 @ 7:43 pm
thanks for your tutorial…
Comment by laabroo — December 25, 2010 @ 2:08 pm
Gud One…Useful post…
ThanQ
Comment by Ash — February 1, 2011 @ 9:27 am
Thanks!
_o/
Comment by Fred — February 9, 2011 @ 11:12 am
Just what i was looking for.. Thanks
Comment by saiful103a — February 9, 2011 @ 6:31 pm
awesome. thanks a lot
Comment by devashish — February 18, 2011 @ 10:53 am
Thanks for the code snippet, very useful.
For anyone who needs to pass custom objects, Bundle works with any class implenting Parcelable interface, so you will neeed to implement such interface in your custom class.
–> http://developer.android.com/reference/android/os/Parcelable.html
Hope it helps
Comment by Alex — June 8, 2011 @ 5:00 am
thank 4 the useful code
Comment by bratin — June 9, 2011 @ 2:25 pm
awesome. this code snippet is very usefull
Comment by indela — July 15, 2011 @ 1:10 am
The sample code is really helpful. Thanks a lot :)
Comment by Amol — July 20, 2011 @ 7:18 am
really the sample code is really nice and easy to understand. its reallyhelpfull. thanks so much :)
Comment by kaan — August 21, 2011 @ 9:58 pm
Exactly what I need! Simple to understand too. Thumbs up!
Comment by Fred — September 20, 2011 @ 5:13 am
thanks a lot.. :)
Comment by karno — October 2, 2011 @ 3:45 am
Thanks a lot….. :)
Comment by hossein — October 9, 2011 @ 4:50 am
Great! Thanks a lot for the clear and straightforward explanation ;-)
Comment by Hartmut — October 27, 2011 @ 9:58 am
Many thanks. This helped me a lot!
Comment by Ouie — November 3, 2011 @ 9:21 am
Informative article..
Thanks for sharing.
Comment by Deleep Rao Pavana — December 21, 2011 @ 7:31 am
Short and clear. I’ve solved my problem with this code.
Comment by Иван Бишевац — December 29, 2011 @ 12:18 pm
Oh yeah! many many thanks for that sharing.
Comment by Sly Senechal — January 2, 2012 @ 4:04 pm
This is exactly what I was looking for. Thank you.
Comment by Zane — March 3, 2012 @ 1:42 am
Thanks for sharing! That was really useful=)
Comment by Rasha — May 8, 2012 @ 4:31 am
GOD BLESS YOU YOU ARE A GENIUS I have search something like this the whole last week, this ist working first time!!! Thanks Alot
Comment by Alex — November 5, 2012 @ 1:05 pm