0

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!

Community
  • 1
  • 1
user1414682
  • 147
  • 3
  • 3
  • 10
  • 5
    So, what have you tried so far? – Lalit Poptani Jun 11 '12 at 10:58
  • 1
    Are you starting your second activity using any `Intent` method like `startActivity(intent)` If yes just refer these [Data Passing](http://stackoverflow.com/q/2091465/940096) and [Passing Data](http://stackoverflow.com/questions/2965109/passing-data-between-activities-in-android) – Praveenkumar Jun 11 '12 at 11:00
  • 2
    I do no understand as to why people give the examples of `putExtra` and `getExtra` in such situations,when simply accessing variables through `getters` and `objects` is what should be encouraged more. – Kazekage Gaara Jun 11 '12 at 11:10
  • And may be for such situations, use global application variables or even some singleton instance of a utility class. – C-- Jun 11 '12 at 11:15

9 Answers9

3

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 :(
        }
    }
}
Will Richardson
  • 7,780
  • 7
  • 42
  • 56
2

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();

   }
}
Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
  • This IS the right answer. If the question is "how can I open an activity which receive var from the caller", this is solved by bundle. But let's say you have (eg) a layer with some buttons (activity1) and in the middle a frame with a custom view (actvity2), and you want to SHARE value: clic on button to change color (button in activity1) and draw with this color in activity2, Paulius's answer IS the best. You CANT use static as the content of var in the second activity will not be refreshed when you'll change is in first actvity. – Peter Sep 17 '16 at 13:42
  • Just a detail: I say you cant' use static, meaning you cant' just set a var as static in activ1 and only read is "statictly" in activ2. It seems you always need to ASK the value from the owner so from activ1. – Peter Sep 17 '16 at 13:53
1

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);
    ...... 
Vyacheslav Shylkin
  • 9,741
  • 5
  • 39
  • 34
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;
}
Bay
  • 467
  • 7
  • 22
andunslg
  • 781
  • 14
  • 38
  • Don't work. In fact if you change the value in the first activity, the change is not reflected in the second one. The only way seems to 'ask' the value to the first activity, as in the Paulius's answer – Peter Sep 17 '16 at 13:51
0

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 :)

maksimov
  • 5,792
  • 1
  • 30
  • 38
0

you can pass the first activity data to second activity using intent.putExtra(yourData);

Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
0

Make the variable static as it is shared by all classes.

Santosh
  • 611
  • 2
  • 6
  • 24
0

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);
anoop
  • 782
  • 4
  • 12
  • 35
0

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.
Yogesh Somani
  • 2,624
  • 3
  • 21
  • 34