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 {
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.
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.
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.
Bundle in android is used to pass data from one activity to another, it takes data in key and value pairs.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With