Description and usage of Android-bundle

Source: Internet
Author: User

The role of the 1.Bundle class

The bundle class is a data carrier, similar to map, that holds the value of the Key-value name value pair form.   Compared to map, it provides a variety of common types of putxxx ()/getxxx () methods, such as: Putstring ()/getstring () and Putint ()/getint (), putxxx () for placing data into bundle objects, GetXxx () is used to fetch data from the bundle object. The inside of the bundle is actually using a variable of type HashMap to hold the value placed by the Putxxx () method. A bundle is a package that is dedicated to importing intent-pass values.

Two basic uses of 2.Intent:

① is an explicit intent, that is, when constructing intent objects, the receiver is specified, which is similar to normal function calls, except that the granularity of reuse is different;

② another is the implicit intent, that is, the intent sender in the construction of the intent object, do not know and do not care who the receiver, this way and function calls are very different, it is helpful to reduce the coupling between sender and receiver.

3.Intent Direct value and pass-through bundle after the value of the comparison ① to jump from aactivity to bactivity need to write 2 intent, if the value involved, intent also write two times to add the value of the method. ② If you use 1 bundles to store the value directly inside and then save it to intent, the code will appear more concise.

requirement : If I have Activity A, B, C now, I'm going to pass the value through a B to C.

Analysis: How do you preach? If use intent words a->b first write once again in B all take out and then in the value plug into intent and then jump to C tired? If I'm using bundles in a, I'm going to pass the bundle to B in B and then to C C so I can go straight to that. There is also a benefit of adding a new key-value to the bundle object in B, which can also be removed in C.

Let's take a look at intent two additional data:

The first way to add data to intent in bulk:

new Intent();  Bundle bundle = new Bundle();//该类用作数据载体  bundle.putString("name","Mike");  intent.putExtras(bundle);//可追加额外的数据,key同名的数据会被替换,与Map用法类似

The second way: not through the bundle, the data are added to the intent, which is more convenient to use, the code is more concise.

new Intent();  intent.putExtra("name","XXX");//不是putExtras

After passing the data, now look at how to take intent and bundles out:

① Direct use of this.getintent () will be able to get the intent,

② then calls Getextras () on the basis of this intent to get bundles.

③ can get data from bundles, depending on the data type. such as String str=bundle.getstring ("name"), get the string with the key "name", int num=bundle.getint ("code"), and get the integer type with the key "code".

In addition, we must pay attention to the difference between Getextras and Getextra. Band S is used for bundles to add values and values.

Description and usage of Android-bundle

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.