Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android: How do I get variables/data from one screen to another?

Tags:

android

In android: I'm trying to take data from one activity/screen to another.

Let's say I'm adding two numbers. I layout my first screen (xml) with 2 EditText views, a couple labels, and an 'OK' button. Now, I want to add the numbers that I've input into the EditText views. Let's say I input 2 and 2 (2 + 2 = 4).

Now, when I hit the 'OK' button, I want a new screen/activity to appear and simply show me the answer (4). Do I use global vars to do this? Any help would be appreciated.

like image 555
Allan Avatar asked Nov 27 '22 12:11

Allan


1 Answers

First Activity

Intent myIntent = new Intent();
myIntent.putExtra("key", "value");
startActivity(myIntent); 

New Activity

Intent myIntent = getIntent(); // this is just for example purpose
myIntent.getExtra("key");

Check out the different types you can use on the Android Dev Site

Note: If you're looking for a way of sharing an object/data globally then you could extend the Application class. Check out How to declare global variables in Android? (answer by Soonil)

like image 154
Ally Avatar answered May 17 '23 23:05

Ally