The difference between intent transfer value and bundle value in Android _android

Source: Internet
Author: User
Tags gettext

For example, I'm going to jump to the B interface or the C interface from the A interface now.
In that case, I need to write 2 intent. If you still want to involve the value of the intent, you will write two times to add the value of the method so if I use 1 bundle directly to save the value first and then save to the intent is not more concise?

Another example if I now have activity A, B, C;
Now I'm going to pass a value through a B to C.
How do you preach if you use intent words a-b first, and then in B, and then in the value of the intent to jump to C tired?
If I use Bundle in a, I'll pass the Bundle to B, then transfer it to C C in B, and I can go straight.
Another benefit is that you can also add a new key-value to the bundle object in B, which can also be taken out of C.

Android provides a intent mechanism to facilitate interaction and communication between applications, or, more accurately, intent not only for applications, but also for activity/service interactions within applications. Intent the meaning of this English word is "purpose, intent", for programmers who are less likely to work on large platform development, this may be an abstract concept that is not easy to understand because it is not quite the same as the simple function/method call We normally use, or the way the interface is invoked through the library as mentioned in the previous section. In the use of intent you do not see a direct function call, the relative function call, intent is a more abstract concept, the use of intent implemented software reuse granularity is activity/service, more than the function of multiplexing, and also more loosely coupled.

Android is related to intent and action/category and intent filter, and also for broadcast intent, these elements are mixed together, causing beginners not too easy to quickly grasp the use of intent. Before we explain these nouns, let's take a look at some basic usages of intent in the following example, see what it can do, and then think about the meaning behind the mechanism.

One of the keys to understanding intent is to understand the two basic uses of intent: One is the explicit intent, that is, when the intent object is constructed, the receiver is specified, which is similar to a normal function call, except that the granularity of the reuse is different; The other is the implicit intent, that is, intent sender in the construction of intent objects, do not know and do not care who the receiver is, this way and function calls are very different, which is conducive to reducing the sender and receiver coupling between. In addition to sending, intent can also be used for broadcasting, which will be described in detail in the following text.

Intent and bundle implement code examples for converting from one activity band parameter to another

Copy Code code 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);
锟 the activity of the pound
StartActivity (Intent);
}

To remove bundle parameters from another activity
Copy Code code 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);
}
});


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.