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 the two basic usage of intent: an explicit intent, that is, the receiver is specified when the intent object is constructed, this method is similar to common function calls, but the granularity of reuse is different. The other is implicit intent, that is, when the intent sender constructs the intent object, I don't know and don't care who the receiver is. This method differs greatly from function call, which helps reduce the coupling between the sender and the 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
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 activity
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 ){
Intent intent = new intent ();
Intent. setclass (informationactive. This, mainactive. Class );
Startactivity (intent );
}
});
}