What is the difference between Bundle and Intent?

Firstly, this question may seem really absurd for some Android veterans, but as a newbie, I really need to ask this question:

Intent is a passive data structure that transfers information from one Activity to another. Intent can also store data in the form of name-value pairs (via putExtra() ).

But, overriding the onCreate() method, we pass the Bundle as a parameter, which ultimately also contains values ​​in the form of name-value pairs and can store information using onSaveInstanceState() .

In such a scenario, why do we need both that distinguishes them?

UPDATE

I suppose I led you guys into a misunderstanding that I misunderstood what Intent :

When I said: " Intent is a passive data structure that transfers information from one Activity to another," I would like to point out that even Intent can carry information (except for the description of the context and the action) using the putExtra() method. Why should we use a Bundle then?

Also, please make sure that you leave the reason in the comments below if you omit / raise a question. Thanks for the generosity.

+7
android android-intent bundle
source share
4 answers

I think you already understand what a Bundle : a collection of key-value pairs.

However, the intention is much greater. It contains information about the operation to be performed. This new operation is determined by the action for which it can be used, and the data that it should show / edit / add. The system uses this information to find the appropriate application component (activity / translation / service) for the requested action.

Think of the intention as a bundle, which also contains information about who should receive the data contained in it and how it should be presented.

+4
source share

I really don't know where you got this Intent definition from, but as the definition of "Intent"

The goal is an abstract description of the operation being performed. It can be used with startActivity to start an Activity, broadcastIntent, to send it to any interested BroadcastReceiver components and startService (Intent) or bindService (Intent, ServiceConnection, int) to communicate with the background service.

Anent provides a means to do late-time runtime binding between code in different applications. Its most significant use is in launching activities, where it can be seen as the glue between actions. This is basically a passive data structure containing an abstract description of the action to be performed.

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

In Intent, you will find a definition for Advanced

extras is a bundle of any additional information. This can be used to provide extended information to a component. For example, if we have an action to send an email message, we can also include additional pieces of data to provide a subject, body, etc.

So that means Extras in Intent is an A Bundle object

The transition to the Bundle , since you mentioned that it is the medium for data from one intention to another and is a Key-Value variable map.

+3
source share

Intent facilitates the exchange of data between components. Intent is a message that is passed between components such as activity. which you can use aim.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 the bundle:

 Bundle bundle=new Bundle(); bundle.put(key,value); intent.putExtra(bundle); startActivity(intent); 

Call a bunch in another action:

 Bundle extras=getIntent().getExtras(); extras.getString(key); 
+3
source share

From the source of the Intent class, there is no difference between the two. Check out the code from the 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 the only difference is ease of use .. :) for the 1st, you don’t need to explicitly create your package.

+2
source share

All Articles