The effect is as follows:
Initial interface
Enter information
After "OK"
First, the API document description
1. Introduction
For data transfer between different activity
1. Important Methods
Clear (): Clears all saved data from this bundle map.
Clone (): Clone current Bundle
ContainsKey (String Key): Returns the value of the specified key
GetString (String Key): Returns the character of the specified key
Hasfiledescriptors (): Indicates whether any bundled package file descriptors are included
IsEmpty (): Returns True if this bundle map is empty
Putstring (string key, String value): Inserts a string value for a given key
Readfromparcel (Parcel Parcel): Read the contents of this Parcel
Remove (String key): Removes the value from the specified key
Writetoparcel (Parcel Parcel, int flags): Write the contents of this Parcel
Second, examples
public class Bundledemo extends Activity {
Private EditText Etname;
Button btn;
/*
* (Non-javadoc)
*
* @see Android.app.activity#oncreate (android.os.Bundle)
*/
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.bundle);
Etname = (EditText) Findviewbyid (r.id.etname);
BTN = (Button) Findviewbyid (R.ID.BTN);
Btn.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
String info = Etname.gettext (). toString ();
Bundle bundle = new bundle ();
Save the input information
Bundle.putstring ("name", info);
Intent intent=new Intent (bundledemo.this,bundledemo1.class);
Intent.putextras (bundle);
Finish ();
StartActivity (Intent);
}
});
}
}
public class BundleDemo1 extends Activity {
Private TextView Etname;
/* (Non-javadoc)
* @see Android.app.activity#oncreate (android.os.Bundle)
*/
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.B1);
Etname= (TextView) Findviewbyid (r.id.txtname);
Bundle b=getintent (). Getextras ();
Get information on bundles
String info=b.getstring ("name");
Etname.settext ("Your Name:" +info);
}
}
Three, the difference with the sharedpreferences
Sharedpreferences is a simple storage persistence setting, just like a user's home page each time the application is opened, it is simply a simple key-value pair to manipulate. It saves the data in an XML file
Bundles are data storage methods that pass data to another context or save or reply to your own state. Its data is not a persistent state.
Android Bundle sharedpreferences differences