The use of Android bundle __android

Source: Internet
Author: User
Tags map class
Bundle's Understanding:

A container class that holds the map type of the string and parcelable type data, obtains the corresponding values of various types by holding the Data key (key), and must be obtained by key.


Usage of bundle:

Bundle is equivalent to the map class, is a mapping, with bundle binding data for easy data processing

It mainly acts on the data transfer between the activity.

Testbundle.java

Bundle Bundle = new Bundle ();//Create a handle

Bundle.putstring ("name", nameinfo);//fill nameinfo into handle

Intent mintent = new Intent (testbundle.this,testbundle_getvalue.class);

Mintent.putextras (bundle);

StartActivity (mintent);

Testbundle_getvalue.java

Bundle Bundle = Getintent (). Getextras ()//Get a handle

String namestring=bundle.get ("name");//The value is namestring by the key to "name".

/*

* For related operations

*/

Examples of bundle:

The first activity emits a parameter. Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import android.view.MotionEvent;

public class Testbundle extends activity {
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}

public boolean ontouchevent (Motionevent event) {
Intent Intent = new Intent ();
Intent.setclass (Testbundle.this, Target.class);
Bundle mbundle = new Bundle ();
Mbundle.putstring ("Data", "Hello, bear");
Intent.putextras (Mbundle);
StartActivity (Intent);
Finish ();
Return Super.ontouchevent (event);
}
}

Second activity, receiving parameters

Import android.app.Activity;
Import Android.os.Bundle;

public class Target extends activity{

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Bundle Bundle = Getintent (). Getextras ();
String data=bundle.getstring ("data");/read out data
Settitle (data);
}

}


In addition, the savedinstancestate in OnCreate (Bundle savedinstancestate) is used to save temporary data before the current activity is switched so that the previous data is displayed on the next return. So if you want to use Bundle savedinstancestate to save temporary data, you should write savedinstancestate in advance in the OnCreate (Bundle savedinstancestate) method. =null the logic of the time. Reference Understanding: Http://blog.sina.com.cn/s/blog_652dd96d0100ug6h.html

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.