Possible Duplicate:
Passing data between activities in Android
so I have two activity's, and I need to save one variable in first activity, and use it in the second activity. Can anyone help me? Thanks!
Possible Duplicate:
Passing data between activities in Android
so I have two activity's, and I need to save one variable in first activity, and use it in the second activity. Can anyone help me? Thanks!
Use something like this:
Intent intent = new Intent(this, ClassImCalling.class);
intent.putExtra("variable", myvariable);
startActivityForResult(intent, int_identifier);
And in the other Activity:
intent = getIntent();
var=intent.getStringExtra("variable");
To return to the activity that called it (intent being same as getIntent() above):
setResult(RESULT_OK, intent);
finish();
And when you return back to the first Activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == int_identifier) {
if (resultCode == RESULT_OK) {
Do suff
}else if(resultCode == RESULT_CANCELED){
Action was cancelled :(
}
}
}
Keep it simple. In one class you set the variable's value in other class use that class's instance to get it.
public class Activity1 {
private String var;
public Activity1() {
setVar("some_value");
}
public String getVar() {
return this.var;
}
public void setVar(String var) {
this.var = var;
}
}
public class Activity2 {
public void doSmth() {
Activity1 a = new Activity1();
String varValue = a.getVar();
}
}
First Activity:
....
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(Consts.EXTRA_EDIT_MODE_KEY, 123);
....
intent.putExtra(_some_key_, _some_data_);
intent.putExtra(_some_key_, _some_data_);
startActivity(intent);
SecondActivity:
.....
Intent intent = getIntent();
int mode = intent.getIntExtra(Consts.EXTRA_EDIT_MODE_KEY, -1);
......
As understand your requirement is to use a variable value in several Activity classes. As they mean they are JAVA classes, so you can use a static variable for your task.
Say you have a class like this,
public class Activity1 extends Activity{
static String name="abc";
}
If you want to use that name variable in a other class, you can use,
public class Activity2 extends Activity{
String name2=Activity1.name;
}
Here's one way of doing it, I guess:
class Class1 {
private int someValue = 0
void doSomething(Class2 anotherObj) {
this.someValue = 1;
anotherObj.setValue(this.someValue);
}
}
There's many other ways :)
you can pass the first activity data to second activity using intent.putExtra(yourData);
If we use shared preferance we can use this value in any activity.
Here is the code in ManageSharesPrefs:
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString(MY_NAME, "Sai");
prefsEditor.putString(MY_WALLPAPER, "f664.PNG");
prefsEditor.commit();
and retrive value as
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString(MY_NAME, "nothing");
String wallPaper = myPrefs.getString(MY_WALLPAPER, null);
You can use Bundle object to share a variable across 2 activities. This is how I use it:
(1) First, put the variable as an extra into a bundle. Suppose you have the variable in FirstActivity and you want to send it to SecondActivity, then this is the way to put it(in the FirstActivity):
String variableValue="x"; // it holds some value which you want to pass to SecondActivity.
Intent secondIntent = new Intent(view.getContext(), SecondActivity.class);
Bundle bundleObj = new Bundle();
bundleObj.putString("variableName", variableValue);
secondIntent.putExtras(bundleObj);
startActivityForResult(secondIntent, 0);
(2) This is how we get it in the SecondActivity(in the onCreate() method):
Bundle extras = getIntent().getExtras();
String variableValue = extras.getString("variableName");
** Similarly you can pass the integer and other datatyped values as well.