Problems with sharedpreferences not reading data in Android

Source: Internet
Author: User

In two different activity, a in sharedpreferences saved the data, in a can be read, but in B but not read, has been empty value, good is uncomfortable, because it is according to the example of the book, How can not find the reason, It was later discovered that a name was needed when it was saved before it could be read in another activity.

The example in the book is like this.

Sharedpreferences.editor ed= getpreferences (mode_private). Edit ();

The following code is then read to the

Sharedpreferences sp=getpreferences (mode_private); U=sp.getstring ("user", "T");

There is no problem reading in the same activity, but when read in another activity, the value is not read.

It was later discovered that it was necessary to give it a name when it was saved and then read it by name.

Sharedpreferences sp=getsharedpreferences ("PPPoE", mode_private);

At the time of reading

Sharedpreferences sp=getsharedpreferences ("PPPoE", mode_private); uid= sp.getstring ("User", "1235");

So there is no problem, it seems that the author of the book is not too hard.

Problems with sharedpreferences not reading data in Android

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.