Home » Android » java – How do I get a variable in another activity?

java – How do I get a variable in another activity?

Posted by: admin June 15, 2020 Leave a comment


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,


    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 {
How to&Answers:

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.


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


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:


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.