How to get a variable in another action?

How do I access the value of a variable in another action. In my example, I have a string variable element whose value is chosen equal to spinner. How can I access this variable in another action 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 {
+5
source share
3 answers

You can declare them as static variables , and then in your other class you can access them, for example Activity1.stringName .

public static String stringName; 

stringName = .. // value from Spinner

Then in all other actions you can access them as . YourMainActivty.stringName

+16
source

, , .

public String getMyString(){
    return item;
}

:

String myValue = LoginScreen.getMyString();
+4

.

1. Boundle Application. (ApplicationClass.java)

     public static Bundle mMyAppsBundle = new Bundle():

2:

. :

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

3:

:

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

.

+1

All Articles