Android Bundle sharedpreferences Differences

Source: Internet
Author: User

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

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.