Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a variable in another activity?

Tags:

java

android

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 {
like image 509
Hayya ANAM Avatar asked Sep 05 '12 19:09

Hayya ANAM


People also ask

How can I access a variable from one activity to another?

You can pass primitive data from one activity to another as follows. Define constant which will be used as Key to pass data. public static final String EXTRA_YOUR_KEY = "EXTRA_YOUR_KEY"; // code snippet from second activity's onCreate method Intent intent = getIntent(); long id = intent.

How do I make a button go to another activity?

Using an OnClickListener Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity . Button yourButton = (Button) findViewById(R. id.

How can I pass value from one activity to another activity in Android without intent?

This example demonstrate about How to send data from one activity to another in Android without intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you pass data from one activity to another activity in Kotlin?

Bundle in android is used to pass data from one activity to another, it takes data in key and value pairs.


1 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.

like image 171
Swayam Avatar answered Sep 28 '22 15:09

Swayam