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 ano
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:
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.