1

In my app I send some intent extras from one activity to another. But some users report back that these data are always zero - even though I can see the values are alright in the sending activity.

Here's the code of the sending activity:

Intent intent = new Intent();
intent.setClass(waypointListView.this, addWaypointActivity.class);
intent.putExtra("latitude", String.format("%9.6f", globLatitude));
intent.putExtra("longitude", String.format("%9.6f", globLongitude));
startActivityForResult(intent, ACTIVITY_ADD_WAYPOINT);

And this is how it's read in the new activity:

Intent myIntent = getIntent();
String latitudeStr = myIntent.getExtras().getString("latitude");

try{
  globLatitude = Float.parseFloat(latitudeStr);
} catch(NumberFormatException nfe) {    
  globLatitude=0f;
}

String longitudeStr = myIntent.getExtras().getString("longitude");

try{
  globLongitude = Float.parseFloat(longitudeStr);
} catch(NumberFormatException nfe) {    
  globLongitude=0f;
}

On both my devices it works fine, but I have 3 cases of customers complaining that it doesn't work (documented in video recordings).

Any suggestions?

user1057831
  • 211
  • 1
  • 11
  • 2
    Any particular reason you are using strings instead of floats as your extras? `putExtra()` takes a `float`; `getFloatExtra()` returns a `float`. Beyond that, extras do not magically vanish in calls to `startActivity()` or `startActivityForResult()`. You need to work out the states of your state machine and determine what path is invoking your second activity where those extras would not have been attached. – CommonsWare Jun 02 '12 at 11:42
  • No - no reason for not using float - but I guess that shouldn't make any difference. There is only one place where the second activity gets called, and always with the extras set. – user1057831 Jun 02 '12 at 18:52

1 Answers1

0

I tried to change the code to use getFloatExtra() instead of getString and parse it to a float, and it solved the problem. I see this is a lot more efficient, but I still don't understand why the original solution worked on some devices but not on others.

Case closed!

user1057831
  • 211
  • 1
  • 11