0

Possible Duplicate:
How do I pass data between activities in Android?

I have been banging my head across the table for days trying to figure this out. I'm new to both Java and the Android platform and I am writing my second application. In a nutshell, I want the user to enter in some information in the first activity. That activity then takes those inputs and runs a series of mathematical computations and then displays the results in several editTexts in a separate activity. In my mind, the application takes the input, runs the computations and then stores them in some variables. The variables then need to be passed to the second activity which then displays the results. I have most of the application coded and I can get it to work properly if I keep the inputs and displayed results in one activity. Any Ideas on how I can get the displayed results to show up on another activity? Any direction here is much appreciated.

Community
  • 1
  • 1
chris
  • 85
  • 2
  • 11

6 Answers6

3

This can be done with use of intent. one of the use of Intent is to pass the data between activities. In your scenario what you need to do is

STEP 1

After taking input from the user, do computation, store result in the variables
bundle that in the intent which you are using to start next activity. You can achieve this by below code

Intent intent = new Intent();
            intent.putExtra("KeyForResult1", <your variable>);
            intent.putExtra("KeyForResult2",<your variable>);
            startActivity(this, nextactivity.class);

in the nextactivity you need to get the intent and extract the values in the variable which can be achieved

variabletype variable = getIntent().getExtras().get("KeyForResult1");
   variabletype variable = getIntent().getExtras().get("KeyForResult2");
Janusz
  • 187,060
  • 113
  • 301
  • 369
Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
1

You can use this:

ActivityOne.class:

//compute the data and get the result here
//suppose results are,

    int resultInt=24;
    String resultString="abc";

    Intent intent=new Intent(getApplicationContext(),ActivityTwo.class);
    intent.putExtra("ResultInInteger",resultInt);
    intent.putExtra("ResultInString",resultString);
    startActivity(intent);

this will open ActivityTwo.class,where you can get the data like:

int resultInt=getIntent().getIntExtra("ResultInInteger");
String resultString=getIntent().getStringExtra("ResultInString");
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • Maybe I am doing something wrong, but when I add the code in the second class, I keep getting an error on the "getIntExtra" snippet. I get "the method getIntExtra(String, int) in the type Intent is not applicable for the arguments (String)" any thoughts? – chris Nov 26 '11 at 04:02
  • Yup...my mistake! you should provide default value in getting Integer Extra from an intent in 2nd activity.So use int resultInt=getIntent().getIntExtra("ResultInInteger",0); instead,in above answer. – Hiral Vadodaria Nov 28 '11 at 04:15
1

I was able to figure it out with a mixture of data I had researched. It seems what finally worked was

int resultInt=24;

Intent myIntent = new Intent(CalPalActivity.this, ResultsActivity.class);
myIntent.putExtra("intVariableName", resultInt);
startActivity(myIntent);

in the first activity and

Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);

in the second. Why it worked like this and kept giving me errors such as "the method getIntExtra(String, int) in the type Intent is not applicable for the arguments (String)" in the second activity I'll never know. Even the Quick-fixes didn't fix it (I just kept getting new errors). All in all, it's an expensive (5 hour +) but well learned lesson. All of your help was much appreciated. I was able to understand and learn a great deal more about the language. Thank you!

chris
  • 85
  • 2
  • 11
0

You should read up on Intents. You can store extras in an intent, much like URL parameters. If you use that intent to start your next activity, it will be able to extract the data from the intent's extras.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
0

There are several options. For your task I'd choose passing the data in Intent's extras.

For all options read this

a.ch.
  • 8,285
  • 5
  • 40
  • 53
0

Here is an option. In first Activity:

Intent yourNextActivityIn=new Intent(this,yourNextActivity.class);
yourNextActivityIn.putExtra("tag",valueToBePassed);
startActivity(yourNextActivityIn);

In second Activity:

//If value is an Integer with default value 1
int value = getIntent().getIntExtra("tag", 1);

else

//if value is a string
String value = getIntent().getStringExtra("tag");   
freshDroid
  • 509
  • 1
  • 5
  • 16