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);
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).
source share