Hayya ANAM
Hayya ANAM

Reputation: 575

How do I get a variable in another activity?

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 {

Upvotes: 11

Views: 66272

Answers (5)

Bhavin Solanki
Bhavin Solanki

Reputation: 510

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 
          
       }

}

Upvotes: 0

Nitin Vats
Nitin Vats

Reputation: 51

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.

Upvotes: 0

Mbhammerbro
Mbhammerbro

Reputation: 194

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

Upvotes: 7

KulArtist
KulArtist

Reputation: 962

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.

Upvotes: 3

Swayam
Swayam

Reputation: 16354

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.

Upvotes: 28

Related Questions