4

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.

Allan
  • 1,023
  • 4
  • 13
  • 20

4 Answers4

23

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)

Community
  • 1
  • 1
Ally
  • 2,195
  • 1
  • 16
  • 29
4

I suppose that You're starting "next screen" using Intent (it's the way it should be done).

In Intent you can pass extras (putExtra) and in onCreate in "next activity" you can getIntent().getXExtra() (substitute X with field type)

skyman
  • 2,422
  • 17
  • 16
2

Take a look at the Some Intent examples section (from Common Tasks and How to Do Them in Android):

basically, you use myIntent.putExtra (...) to send data (can be String, Int, Boolean etc) to the other receiving end (the other activity)...

then, result will be passed back into the calling Activity's onActivityResult() method:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    // See which child activity is calling us back.
    switch (resultCode) {
        case CHOOSE_FIGHTER:
            // This is the standard resultCode that is sent back if the
            // activity crashed or didn't doesn't supply an explicit result.
            if (resultCode == RESULT_CANCELED){
                myMessageboxFunction("Fight cancelled");
            } 
            else {
                myFightFunction(data);
            }
        default:
            break;
    }

H.

Hubert
  • 16,012
  • 18
  • 45
  • 51
  • The above is for an activity result but I think you want to move data from 1 activity to another e.g. myIntent.putExtra("key", "Value"); then in the new intent you use Intent.getExtra("key") There are more putExtra() for other types – Ally Feb 27 '10 at 11:52
  • yes indeed, and honestly sometimes I also use global vars and/or the Preferences to store data that I need in other activities. – Hubert Feb 27 '10 at 12:12
  • Preferences are not recommended, because they create a significant overhead. In fact, the recommended approach to deal with preferences is to put them in a seperate thread. – Michał Klimczak Apr 19 '12 at 16:43
0

A sample from my project. We have to use Bundle to get the data.

Use the code in the from/first activity yo set the data. You can set all kind of data types including arrays.

Intent i = new Intent(this, LanguageSetting.class);
i.putExtra("From", 1);
startActivity(i);

How to retrive the data, write the below code in the new/second activity.

Intent myIntent = getIntent();
Bundle b = myIntent.getExtras();
intCameFrom = b.getInt("From");
Joseph Selvaraj
  • 2,225
  • 1
  • 21
  • 21