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