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

前端 未结 3 1645
南笙
南笙 2021-01-02 14:17

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

相关标签:
3条回答
  • 2021-01-02 14:37

    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")
    
    0 讨论(0)
  • 2021-01-02 14:40

    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)
    
    0 讨论(0)
  • 2021-01-02 14:45

    Look at source code http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r1.2/android/content/Intent.java#Intent.putExtras%28android.os.Bundle%29

    0 讨论(0)
提交回复
热议问题