简体   繁体   中英

Intent.putExtra(String,Bundle) vs Intent.putExtra(Bundle)

This question may sound stupid but I wana know When do we put activity name in Intent.putExtra() ? In one case we are putting extra only with bundle and in other case we are passing it with class name. I am a little confused should we use Intent.putExtra(String, Bundle) we already have passed the activity name in Intent constructor or not?

Thanks for your help!

I think you mean putExtra(String, Bundle) vs putExtras(Bundle) (with s ).

The first adds the bundle as the value for the key you provide. The bundle is simple an object value.

The second adds all the key/value pairs from the provided bundle to the intent. In this case the content of the bundle will be added to the intent, not the bundle itself.

Think of them as in Map interface:

Map.put(String key, Object value)

vs

Map.putAll(Map anotherMap)

The approach is just the difference here. If you use a Bundle you can store almost all types in it:

Bundle mBundle = new Bundle();
mBundle.put(key, value);

and pass it to an activity

mIntent.putExtras(mBundle);

and in the other activity which recieves the info, just grab the content of the bundle like this:

   Bundle extras = getIntent().getExtras();

and grab each element in the bundle like this:

extras.getString("myKey")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM