Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Integer Between Activities and Intents in Android Is Always Resulting in Zero / Null

I'm attempting to pass two integers from my Main Page activity (a latitude and longitude) to a second activity that contains an instance of Google Maps that will place a marker at the lat and long provided. My conundrum is that when I retrieve the bundle in the Map_Page activity the integers I passed are always 0, which is the default when they are Null. Does anyone see anything glaringly wrong?

I have the following stored in a button click OnClick method.

Bundle dataBundle = new Bundle();

dataBundle.putInt("LatValue", 39485000);
dataBundle.putInt("LongValue", -80142777);
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtras(dataBundle);
startActivity(myIntent);

Then in my map_page activity I have the following in onCreate to pick up the data.

Bundle extras = getIntent().getExtras(); 
System.out.println("Get Intent done");
if(extras !=null)
{
    System.out.println("Let's get the values");
    int latValue = extras.getInt("latValue");
    int longValue = extras.getInt("longValue");

    System.out.println("latValue = " + latValue + " longValue = " + longValue);

}
like image 504
Geeklat Avatar asked Mar 24 '11 18:03

Geeklat


People also ask

How can we pass integer value from one activity to another activity?

putString("StringVariableName", intValue + ""); intent. putExtras(extras); startActivity(intent); The code above will pass your integer value as a string to class B. On class B, get the string value and convert again as an integer as shown below.

Can intent be null?

It's possible to change intent from outside of activity with setIntent() . In all other cases it can't. Show activity on this post. It CAN be null when Your application was updated from the market while it was in the memory and relaunched again after the update.

How do you pass an activity in intent?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);

How can I get integer value from one activity to another in Android?

//First Activity Intent i = new Intent(this, SecondActivity. class); i. putExtra("MY_KEY", 15); startActivity(i); //Second Activity int number = getIntent(). getExtras().


2 Answers

Geeklat,

You don't need to use Bundle in this case.

Do your puts like this...

Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
startActivity(myIntent);

Then you can retrieve them with...

Bundle extras = getIntent().getExtras();
int latValue = extras.getInt("LatValue");
int longValue = extras.getInt("LongValue");
like image 114
Will Tate Avatar answered Oct 20 '22 00:10

Will Tate


System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");

Not the same as

myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);

Also it might be because you do not keep the name of the Int exactly the same throughout your code. Java and the Android SDK are Case-sensitive

like image 23
Taranasus Avatar answered Oct 20 '22 01:10

Taranasus