27

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?

E_net4
  • 27,810
  • 13
  • 101
  • 139
ikartik90
  • 2,695
  • 6
  • 26
  • 38
  • Read this http://stackoverflow.com/questions/4999991/what-is-a-bundle-in-an-android-application#autocomment35012928 – Trupti Apr 08 '14 at 12:01
  • The link above misses the point of this question, which is to differentiate between data passing with an Intent and data passing with a Bundle. If you've come this far, you've already visited that link. – Donal Lafferty Jul 07 '15 at 08:24

4 Answers4

18

I think you already have understood what a Bundle is: a collection of key-value pairs.

However, an Intent is much more. It contains information about an operation that should be performed. This new operation is defined by the action it can be used for, and the data it should show/edit/add. The system uses this information for finding a suitable app component (activity/broadcast/service) for the requested action.

Think of the Intent as a Bundle that also contains information on who should receive the contained data, and how it should be presented.

FD_
  • 12,947
  • 4
  • 35
  • 62
  • 3
    Updated the question. Sorry for the inappropriate description before. Coming to your answer, but that's what I intend to ask- "Why do we need a `Bundle` when we already have the same features available with an `Intent`?" – ikartik90 Apr 08 '14 at 11:42
  • 4
    Because you sometimes need a collection of key-value pairs that don't have the pre-defined signification the fields of the Intent have. You often need some kind of dictionary in a place where it is not related to starting an action in any way. – FD_ Apr 08 '14 at 11:45
13

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.

Farhan
  • 13,290
  • 2
  • 33
  • 59
4

Intent facilitate communication between components.Intent is the message that is passed between components such as activity. that can be used intent.putExtra(Key,value) and intent.putExtra(Bundle)

Intent intent = new Intent();

intent.setClass(this, Other_Activity.class);
// intent.putExtra(key,value)
intent.putExtra("EXTRA_ID", "SOME DATAS");
startActivity(intent);

Using Bundle : For example

Bundle bundle=new Bundle();
bundle.putString("Key","Some value");
intent.putExtras(bundle);
startActivity(intent);

Call the bundle in another activity :

Bundle extras=getIntent().getExtras();
extras.getString(key);
dilip
  • 63
  • 2
  • 10
Praveen Kumar Verma
  • 2,988
  • 2
  • 18
  • 31
3

I really don't know from where you got this definition for Intent, but as an 'Intent' definition

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

So Intent is an action to link to new (Activity, Service, BroadCastReceiver)

In Intent you will find a definition for Extras

extras -- This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.

So that means Extras in the Intent is an object of A Bundle

Going to Bundle as you mentioned it is a carrier for data from one Intent to another and is a map of Key-Value variables.

Omar HossamEldin
  • 3,033
  • 1
  • 25
  • 51
  • Please re-read the question and revert. I've updated the description. – ikartik90 Apr 08 '14 at 11:35
  • But then why is the return type of `putExtra()` an `Intent` and not a `Bundle`? Please tollerate my foolish questions for a while further and provide me answers. Thanks. – ikartik90 Apr 09 '14 at 10:30
  • Returning a `Bundle` this `Bundle` will contains only the extra that you just added. But this function returns an `Intent` as mentioned in the developers documentation *Returns the same Intent object, for chaining multiple calls into a single statement.* which means you can put more than extra in same statement like `intent.putExtra(extraObject).putExtra(ExtraObject)` etc....., Feel free to ask this the purpose of this forum – Omar HossamEldin Apr 09 '14 at 12:19