SoFunction
Updated on 2025-03-04

Detailed explanation of the difference between Intent pass value and Bundle pass value in Android

For example, I now want to jump from interface A to interface B or interface C.
In this way, I need to write 2 Intents. If you want to pass values ​​in it, your Intent will have to write the method of adding values ​​twice. Then if I use 1 Bundle and directly save the value in it first and then in the Intent, wouldn’t it be more concise?

Another example if I now have Activity A , B , C;
Now I want to pass the value through A to C via B to C
How do you pass it if you use Intent, write A-B first and then take it out in B and then stuff the value into Intent and then jump to C. Are you tired?
If I use Bundle in A, I will pass Bundle to B and then transfer to C. Then I can go directly
In this case, there is another advantage that in B, you can also add a new key - value to the Bundle object.

Android provides an Intent mechanism to assist in the interaction and communication between applications, or to put it more accurately, Intent can not only be used between applications, but also between activities/Services within applications. The original meaning of the English word Intent is "purpose, intention", etc. For programmers who are less engaged in large-scale platform development, this may be an abstract concept that is not easy to understand, because it is different from the simple function/method calls we usually use, or the way the interface is called through the library mentioned in the previous section. You can't see direct function calls in the use of Intent. Compared with function calls, Intent is a more abstract concept. The granularity of software reuse implemented using Intent is Activity/Service, which is higher than function reuse, and the coupling is also looser.

Intent related to Android are also Action/Category and Intent Filter, and Intent for broadcasting. These elements are doped together, making it difficult for beginners to quickly master the usage of Intent. Before explaining these nouns, let’s first experience some basic usage of Intent from the following examples, see what it can do, and then think about the meaning behind this mechanism.

One of the keys to understanding Intent is to understand theTwo basic usagesOne is an explicit Intent, that is, when constructing an Intent object, the receiver is specified. This method is similar to ordinary function calls, except that the granularity of multiplexing is different;The other is the implicit Intent, that is, when the sender of the Intent constructs the Intent object, he does not know or care about who the receiver is. This method is quite different from function calls, which is conducive to reducing the coupling between the sender and the receiver. In addition to sending, Intent can also be used for broadcasting, which will be described in detail later.

Example of code of Intent and Bundle implementation of conversion from one Activity with parameters to another Activity

Copy the codeThe code is as follows:

if(et_username.getText().toString().equals("peidw") && et_password.getText().toString().equals("123456") ){
    intent = new Intent();
                   Bundle bundle = new Bundle(); 
                   ("USERNAME", et_username.getText().toString()); 
                   (bundle);
                   (, );
                   startActivity(intent);
}else{
                   intent = new Intent(); 
                   (, ); 
//Kunjinkang Activity
                   startActivity(intent); 
}

Take out the Bundle parameters in another activity
Copy the codeThe code is as follows:

protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
();
tv = (TextView)findViewById(.first_page_info);
Bundle bundle = ().getExtras(); 
String str=("USERNAME");
(str);
button_back = (Button)findViewById(); 
button_back.setOnClickListener(new OnClickListener() { 
         public void onClick(View view) {
                Intent intent = new Intent();
                (,);
                startActivity(intent); 
         }
   });

 
   }