Android tips: Bundle and sharedpreferences of data transfer between activity

Source: Internet
Author: User

1. Introduce

For beginners The data transmission between different activity of Android is always a difficult problem, the main solution is to use bundle to transmit data, one is to use sharedpreferences. The difference between the two, generally speaking, sharedpreferences is used to store lightweight data, stored in XML, and can be persisted. Anti-bundle can transmit a lot of data, but it doesn't last.

2. The concrete realization Method Bundle

In sender class A

Bundle Bundle = new Bundle ();  
      
Saves the input information  
   bundle.putstring ("String Name", "String Transmitted");  
   Intent intent=new Intent (a.this,b.class);  
  Intent.putextras (bundle);

In receiver class B

Bundle b=getintent (). Getextras ();  
  Gets the bundle information  
  string info=b.getstring ("string name");

Note: string names are the same

Sharedpreferences

Sharedpreferences usage is simple, if you want to edit the content in Sharedpreferences, you need to use the editor object.

In the issue of party a

Sharedpreferences sp = preferencemanager.getdefaultsharedpreferences (GetContext ());   
                        Editor Editor = Sp.edit ();  
                        Editor.putstring ("String variable name", "string content issued");  
                        Editor.commit ();

Receiving Party B

Sharedpreferences sp = preferencemanager.getdefaultsharedpreferences (b.this);  
            String grade = Sp.getstring ("String variable name", "Default value");

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

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.