i read document about getInt() method :
public int getInt (String key)
Returns the value associated with the given key, or 0 if no mapping of the desired type exists for the given key.
Parameters:
key a string
return:
an int value
but i can't get it that what it exactly return.
the ID of key
that is in R.java or no something else???
It returns whatever you've put in that bundle with the same key.
Bundle bundle = new Bundle();
bundle.putInt("KEY", 1);
int value = bundle.getInt("KEY"); // returns 1
It's simply a map/dictionary datatype where you map a string value with something else. If you have other datatypes, you should use the appropriate put/get-methods for that datatype.
Nothing better that with an example
Suppose you have two activities: Activity1 and Activity2 and you want to pass data beetwen then:
Activity1
private static final String MY_KEY = "My Key"
Intent intent = new Intent(Activity1.this, Activity2.class);
Bundle b = new Bundle();
b.putInt(MY_KEY, 112233);
intent.putExtras(b);
startActivity(intent);
Activity 2
private static final String MY_KEY = "My Key"
Bundle b = getIntent().getExtras();
int value = b.getInt(MY_KEY , 0);
//value now have the value 112233
What means "Returns the value associated with the given key, or 0 if no mapping of the desired type exists for the given key." in this example?
Using Bundle you're sending the value 112233 from Activity 1 to Activity 2 using the key "MY_KEY". So, "MY_KEY" is associated with 112233.
As you can see there is a second parameter “0”.
It is the default value. In situation when Bundle doesn’t contains data you will receive “0” (default value).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With