Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

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);

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top