What is the integer returned by getInt (string key) in android.os.Bundle?

i read a document about getInt () method:

public int getInt (String key)

Returns the value associated with this key, or 0 if a mapping for the given key exists of the desired type.

Options:

enter the string

Return:

int value

but I canโ€™t understand what exactly it returns.

key identifier that is in R.java or nothing else.

+5
source share
2 answers

It returns everything that you put in this package with the same key.

 Bundle bundle = new Bundle(); bundle.putInt("KEY", 1); int value = bundle.getInt("KEY"); // returns 1 

It's just a map / dictionary data type where you map a string value to something else. If you have other data types, you should use the appropriate put / get methods for this data type.

+4
source

Nothing better than with an example.

Suppose you have two actions: Activity1 and Activity2, and you want to transfer data:

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

Action 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 

Which means "Returns the value associated with this key, or 0 if there is no matching of the required type for the given key." in this example?

Using the Bundle, you send the value 112233 from Activity 1 to Activity 2 using the MY_KEY key. So, "MY_KEY" is associated with 112233.

As you can see, there is a second parameter "0".

This is the default value. In a situation where the bundle does not contain data, you will get "0" (the default value).

+3
source

All Articles