1

I am trying to transfer my variables form one activity page to another but can't move forward after a certain point(new to android app development), this is part of my code:

    double payback = num/int;
    double money = v*i*t*e/1000;
    startActivity(new Intent(MainActivity.this, Power.class));

what should i do after this to get my variables into class Power..

no0bCoder
  • 95
  • 1
  • 1
  • 11
  • 2
    http://stackoverflow.com/questions/15859445/how-do-you-pass-a-string-from-one-activity-to-another/15859488#15859488 use intents. instead of string pass double – Raghunandan Sep 07 '13 at 08:45

3 Answers3

0

Try it like this

Intent intent = new Intent(MainActivity.this , Power.class);
intent.putExtra(String key , double value);
startActivity(intent);

And in the power activity you can retrieve the value by

Intent intent = getIntent();
double payback = intent.getDoubleExtra (String key, double defaultValue);
Ravi
  • 4,872
  • 8
  • 35
  • 46
0

Use intents

  Intent i = new Intent(MainActivity.this, Power.class)
  i.putExtra("key",money);
  startActivity(i); 

To retrieve

 Bundle extras = getIntent().getExtras();
 if(extras!=null)
 {
           double value = extras.getDoubleExtra ("key",defaultvalue);
 }

http://developer.android.com/reference/android/content/Intent.html

putExtra

public Intent putExtra (String name, double value)

Added in API level 1
Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

Parameters
name    The name of the extra data, with package prefix.
value   The double data value.
Returns
Returns the same Intent object, for chaining multiple calls into a single statement.

getDoubleExtra

public double getDoubleExtra (String name, double defaultValue)

Added in API level 1
Retrieve extended data from the intent.

Parameters
name    The name of the desired item.
defaultValue    the value to be returned if no value of the desired type is stored with the given name.
Returns
the value of an item that previously added with putExtra() or the default value if none was found.
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Try This::

Intent intent = new Intent(MainActivity.this, Power.class);
intent.putExtra("payback",payback );
intent.putExtra("money",money);
startActivity(intent );\

In your Power activity;

     double payback= getIntent().getDoubleExtra (String key, double defaultValue);
Ritaban
  • 763
  • 4
  • 8