For example, I want to jump to interface B or interface C from interface.
In this case, I need to write two intents. If you want to transfer the value involved, your Intent will write the method of adding the value twice. If I use one Bundle to directly store the value first is it more concise to store it in Intent?
In another example, if I have Activity A, B, C;
Now I want to pass the value through A through B to C
How do you say that if Intent is used, the A-B first writes it again and then retrieves it in B, then inserts the value into Intent, and then jumps to C tired?
If I use Bundle in A, I will pass the Bundle to B and then transfer it to C in B.
In this case, a new key-value can be added to the Bundle object in B, which can also be obtained in C.
Android provides an Intent mechanism to assist in interaction and communication between applications. Or, to be more accurate, Intent can be used not only between applications, it can also be used for interaction between Activity/Service in an application. Intent is an abstract concept that is not easy to understand for programmers who are less engaged in large-scale platform development, because it is different from the simple function/method call we usually use, or the method of calling interfaces through libraries mentioned in the previous section. In the use of Intent, you cannot see direct function calls. Compared with function calls, Intent is a more abstract concept. The granularity of software reuse implemented by Intent is Activity/Service, higher than function reuse, and more loose coupling.
In Android, there are Action/Category and Intent Filter related to Intent, and Intent for broadcast. These elements are mixed together, making it difficult for beginners to quickly master Intent usage. Before explaining these terms, let's first take a look at some basic usage of Intent from the example below, and then think about the meaning behind this mechanism.
One of the keys to understanding Intent is to understand IntentTwo basic usage methods: An explicit Intent is used to specify the receiver when constructing an Intent object. This method is similar to a common function call, but the granularity of reuse is different; the other is the implicit Intent, that is, when the Intent sender constructs the Intent object, it does not know or care about who the receiver is. This method is quite different from the function call method, this helps reduce the coupling between the sender and receiver. In addition to sending, Intent can also be used for broadcasting, which will be detailed later.
Sample Code for converting Intent and Bundle parameters from one Activity to another
Copy 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 ();
Bundle. putString ("USERNAME", et_username.getText (). toString ());
Intent. putExtras (bundle );
Intent. setClass (loginactive. this, informationactive. class );
StartActivity (intent );
} Else {
Intent = new Intent ();
Intent. setClass (loginactive. this, errorpageactive. class );
// Copy an Activity
StartActivity (intent );
}
Retrieve Bundle parameters in another ActivityCopy codeThe Code is as follows: protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
This. setContentView (R. layout. informationactive );
TV = (TextView) findViewById (R. id. first_page_info );
Bundle bundle = this. getIntent (). getExtras ();
String str = bundle. getString ("USERNAME ");
TV. setText (str );
Button_back = (Button) findViewById (R. id. back );
Button_back.setOnClickListener (new OnClickListener (){
Public void onClick (View view ){
Intent intent = new Intent ();
Intent. setClass (informationactive. this, mainactive. class );
StartActivity (intent );
}
});
}