Android.content.SharedPreferences.edit ()

Source: Internet
Author: User

Today, when implementing a save user setting to Sharedpreferences, there is a situation where data cannot be saved to sharedpreferences. After careful analysis, it is concluded that:

Android.content.SharedPreferences.Editor.putString (string key, String value)

And

Android.content.SharedPreferences.Editor.commit ()

The different editor used by these two methods is caused.

The error code is as follows:

Sharedpreferences config_sp = getsharedpreferences ("config", mode_private);

Config_sp.edit (). putstring ("Passwd_safe", encodesafepasswd);
Config_sp.edit (). commit ();

The Sharedpreferences.edit () method is described below

/**
* Create A new Editor for these preferences, through which-can make
* Modifications to the data in the preferences and atomically commit those
* Changes back to the Sharedpreferences object.
*
* <p>note that you <em>must</em> call {@link Editor#commit} to has any
* Changes perform in the Editor actually show up in the
* Sharedpreferences.
*
* @return Returns A new instance of the {@link Editor} interface, allowing
* You to modify the values of this Sharedpreferences object.
*/

As the red mark above says, this method creates a new editor object, so two times the editor object is not the same. The result is a data store failure.

Android.content.SharedPreferences.edit ()

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.