11

How do I access variable value in another activity. In my example I have a string variable item which value is spinner selected value. How can I access this variable in another activity without using Intent?

  public class LoginScreen extends Activity {

      Spinner sp;
String item;


      Spinner sp = (Spinner) findViewById(R.id.lgnspinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.network_array,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    sp.setAdapter(adapter);

    sp.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            item = (String) parent.getItemAtPosition(position);



        public class AgAppMenu extends Activity {
Swayam
  • 16,294
  • 14
  • 64
  • 102
Hayya ANAM
  • 575
  • 2
  • 14
  • 38
  • possible duplicate of [Android: How to declare global variables?](http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables). `Activity` life cycle is not in your control and you should not try to control it. See the linked question. – Miserable Variable Sep 05 '12 at 20:07

5 Answers5

28

You can declare them as static variables and then in your other class you may access them like Activity1.stringName.

public static String stringName; 

stringName = .. // value from Spinner

Then, in all the other Activities, you can access them as YourMainActivty.stringName.

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • how to acces in 2nd activity? is not get another activity i use in 2nd activity LoginScreen.stringname is show error – Hayya ANAM Sep 05 '12 at 21:00
  • It should not. Can you please attach the code you are using ? It would help me identify your error. – Swayam Sep 05 '12 at 21:02
7

If you didn't want to use a global variable you could always create a method in your activity to return your string.

public static String getMyString(){
    return item;
}

Then in your current activity you could call:

String myValue = LoginScreen.getMyString();
Holzgraeber
  • 29
  • 1
  • 7
Mbhammerbro
  • 194
  • 1
  • 9
  • 1
    getMyString() must be declared static else can't never access it. – Han Whiteking Feb 24 '18 at 04:22
  • 1
    This answer should be downvoted as it's fully wrong! non-static fields cannot be accessed without class instance! this is the ABCD of the OO-Programming !! – Yahya Mar 12 '18 at 16:23
3

Try this.

Step 1: Create a static Bundle object in Application class.( ApplicationClass.java)

     public static Bundle mMyAppsBundle = new Bundle():

Step 2:

Set key values pair in that bundle from anywhere. like this:

   ApplicationClass.mMyAppsBundle.putString("key","value");

Step 3:

Now you can get these values from anywhere like this way:

   String str = ApplicationClass.mMyAppsBundle.getString("key");

Apply null check before using bundle objects for safety points of view.

KulArtist
  • 965
  • 8
  • 20
0

Using static variables can cause unexpected memory leaks. You should use https://developer.android.com/reference/androidx/localbroadcastmanager/content/LocalBroadcastManager for this. Although it is showing deprecated, you can still use this.

And if you are following the latest pattern. You can Use https://developer.android.com/reference/androidx/lifecycle/LiveData to observe the changes in the variables.

Nitin Vats
  • 51
  • 10
0

Here is something that you exzatly wants.

public class FirstActivity extends AppCompatActivity{

      public static int myVariable = 0;  // Your varible that you want access  

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_first);
          // Your all codes and methods
       }

}

Call varible To your target Activity

public class SecondActivity extends AppCompatActivity{


       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_second);

          FirstActivity.myVariable = 10;    // Call like this to access the myVarible in this activity 
          
       }

}
Bhavin Solanki
  • 418
  • 2
  • 10