Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the integer that return by getInt(string key) in android.os.Bundle?

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???

like image 786
hamid_c Avatar asked Jul 03 '15 15:07

hamid_c


2 Answers

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.

like image 60
Patrick Avatar answered Oct 05 '22 23:10

Patrick


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).

like image 39
Jorge Casariego Avatar answered Oct 06 '22 00:10

Jorge Casariego