tags:

views:

20

answers:

2

Android 2.1 update 1 Eclipse 3.5

I have a problem reading data from my 2nd activity that is called from the 1st activity using intent. I have androidmanifest.xml setup correctly.

My First Activity has the following code:

Intent myIntent = new Intent(MainMenu.this, Testmenu.class); myIntent.putExtra("com.tweaktool.MyAge",40); myIntent.putExtra("com.tweaktool.Enabled", false);
startActivity(myIntent);

My 2nd Activity has the following code:

Bundle bun = getIntent().getExtras();
int myAge = bun.getInt("MyAge");
boolean enabled = bun.getBoolean("Enabled");

When I look at the above code in 2nd Activity it lists the following: enabled = false myAge = 0

Why is this doing this??? Am I doing something simple wrong??

A: 

Have your tried int myAge = bun.getInt("com.tweaktool.MyAge");?

Wesley Wiser
I think I have but why would I need to do that if bun.getBoolean("enabled") works correctly??
barakisbrown
I could be totally wrong about this but isn't 0 the default value for an int just like false is the default value for a bool? I'm still learning my way around java, but this is how it is in c#. I'm guessing that `bun.getBoolean("Enabled")` is returning false because that's a boolean's default value not because it is the value you put in the intent. One way to prove\disprove this would be to try putting true in the intent for "com.tweaktool.Enabled" and see if `enabled` is correct.
Wesley Wiser
your right about the default values. But myage should be the 40 because MyAge was set to 40 in the first activity but its set to 0 not 40.
barakisbrown
I meant the default value of the runtime. I think what's happening is that the data isn't being found so the runtime is returning the uninitialized value for each type, bool->false and int->0. You can see if this is the case by changing `myIntent.putExtra("com.tweaktool.Enabled", false);` to `myIntent.putExtra("com.tweaktool.Enabled", true);` and then seeing if `enabled` is correct.
Wesley Wiser
I have actually set enabled=true and MyAge=0 before I used the above lines of code. It reads the enabled one fine but the myage
barakisbrown
ok. But my point is that `bun.getBoolean("Enabled")` isn't working correctly. Try using the same keys to get the data as you used to set the data.
Wesley Wiser
thanks for the help..I do appreciate it..
barakisbrown
A: 

You're putting data with one keys ("com.tweaktool.MyAge", "com.tweaktool.Enabled") and trying to get it with others ("MyAge", "Enabled") -- the bundle then just returns you defaults (0, false). To get what you've put, use the keys you've used.

Konstantin Burov
I finally figured it out..thanks..
barakisbrown