Search code examples
androidandroid-intentbundle

What is the difference between a Bundle and an Intent?


An Intent is a passive data structure that carries information from one Activity to another. An Intent is also capable of holding data in the form of name-value pairs (via putExtra()). But while overriding the onCreate() method we pass a Bundle as the parameter, which ultimately also holds values in the form of name-value pairs and is able to store information with the help of onSaveInstanceState().

In such a scenario why do we need both and what differentiates the two?

I suppose I have led you guys into a misbelief that I have misunderstood what an Intent is:

When I said "An Intent is a passive data structure that carries information from one Activity to another", what I intended to point out was that even an Intent can carry information (other than the context and action description) with the help of putExtra() method. Why do we need to use a Bundle then?


Solution

  • From the source of Intent class, there really is no difference between the two. Check below code from Intent class:

        public Intent putExtra(String name, String value) {
           if (mExtras == null) {
               mExtras = new Bundle();
           }
           mExtras.putString(name, value);
           return this;
        }
    

    And

        public Intent putExtras(Bundle extras) {
            if (mExtras == null) {
                mExtras = new Bundle();
            }
            mExtras.putAll(extras);
            return this;
        }
    

    So I think, only difference is ease of use.. :) for 1st, you don't need to create your bundle explicitly.