Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a double value through to a different class in Android Java

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

like image 934
Darren Murtagh Avatar asked Mar 06 '12 16:03

Darren Murtagh


3 Answers

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)
like image 171
Kanagalingam Avatar answered Nov 14 '22 03:11

Kanagalingam


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");
like image 20
bschultz Avatar answered Nov 14 '22 02:11

bschultz


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");               
     }
}
like image 21
Gandarez Avatar answered Nov 14 '22 02:11

Gandarez