10

I am just wondering what ways there are to pass two or more double values from classA to ClassB

at the minute i have found code that give me this method:

double a, b;
double a = 2.456;
double b = 764.2353;
Intent i = new Intent();
i.setClassName("com.b00348312.application","com.b00348312.application.ClassB");
double number = getIntent().getDoubleExtra("value1", a);
double number = getIntent().getDoubleExtra("value2", b);
startActivity(i); 

This does not pass the values through nor can i find a way of retrieving these values

Another question on here suggested the method of creating an instance of the class but trying that i cant seem to pass the values through properly.

I am programming for Android, so I don't know if the method will be different

Darren Murtagh
  • 591
  • 2
  • 6
  • 28

5 Answers5

22

You're not actually placing your doubles into your Intent

Intent yourInent = new Intent(thisActivity.this, nextActivity.class);
Bundle b = new Bundle();
b.putDouble("key", doubleVal);
yourIntent.putExtras(b);
startActivity(yourIntent);

Then, get it in your next Activity:

Bundle b = getIntent().getExtras();
double result = b.getDouble("key");
bschultz
  • 4,244
  • 1
  • 25
  • 33
  • Thank you. That really helps. i thought there was a problem with actually putting the values through but i just didn't know what code i needed to put to put it in. Is there any different code if i wanted to pass an int or a float through or is it as simple as chaning .putDouble to .putInteger – Darren Murtagh Mar 06 '12 at 16:58
  • Thank you. That really helps. i thought there was a problem with actually putting the values through but i just didn't know what code i needed to put to put it in. Is there much different if i wanted to pass a float or an int through. – Darren Murtagh Mar 06 '12 at 16:59
  • Np. You would pass a float or int the same way. Dev Docs for [Bundles](http://developer.android.com/reference/android/os/Bundle.html) Should help you. – bschultz Mar 06 '12 at 17:00
  • i seem to have a problem getting the values on the other side. when i put the code in eclipse shows there is an error with the 'Bundle b = getIntent().getExtras();'saything that ";" was needed even though it was already there – Darren Murtagh Mar 06 '12 at 20:56
  • Weird. Make sure it's in your onCreate. Re-build your project and make sure you don't have any semicolons or anything floating around as well. Also, if an answer has helped you, don't forget to mark it. – bschultz Mar 06 '12 at 21:00
  • thats what it was. i thought it would workk in just the class. in the onCreate it works fine. also i cant mark as i'm only new here. it keeps telling me that i need to have 15 reputation to vote. you have been really helpful and once i get the rep i will mark it – Darren Murtagh Mar 06 '12 at 21:18
  • What you're referring to is upvoting. Lol. There is a check mark under the vote count for marking the answer. – bschultz Mar 06 '12 at 21:20
2

You can try by this way

double a, b;
Intent i = new Intent(classA.this, classB.class);

Bundle params = new Bundle();
params.putDouble("doubleA", a);
params.putDouble("doubleB", b);
i.putExtras(params);
startActivity(i);

At other side you need something like this

double a, b;
// Get Params from intent
Intent it = getIntent();        
if (it != null)
{
    Bundle params = it.getExtras();
    if  (params != null)
    {
         a = params.getDouble("doubleA");
         b = params.getDouble("doubleB");               
     }
}
PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
gandarez
  • 2,609
  • 4
  • 34
  • 47
1

In Kotlin,

Sending Activity,

        val returnIntent = Intent()
        returnIntent.putExtra(KeyConstants.LATITUDE, latitude);
        returnIntent.putExtra(KeyConstants.LONGITUDE, longitude)
        setResult(Activity.RESULT_OK, returnIntent);

Recieving Activity,

 val latitude = intent?.getDoubleExtra(KeyConstants.LATITUDE, 0.0)
 val longitude = intent?.getDoubleExtra(KeyConstants.LONGITUDE, 0.0)
Kanagalingam
  • 2,096
  • 5
  • 23
  • 40
0

This is nearly the correct technique for sending information between Activities. You need to use the putDouble() method like so:

i.putDouble("value1", a);
i.putDouble("value2", b);

In order to access these values you need to pull them out of the Extras map on the receiving Activity end like so:

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main2);
     Intent intent=getIntent();
     double a=intent.getExtras().getDouble("value1");
     double b=intent.getExtras().getDouble("value2");
John Ericksen
  • 10,995
  • 4
  • 45
  • 75
0

You need to use:

i.putExtra("number1", number1);
i.putExtra("number2", number1);
Stev_k
  • 2,118
  • 3
  • 22
  • 36