Shared Preferences in android showing Default value not found

前端 未结 4 1215
滥情空心
滥情空心 2021-01-25 15:14

I strucked in storing and getting shared preferences.When a user has been registered if that registration is success then that values has to be stored in his profile nothing but

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-25 16:21

    You've used SharedPreferences.Editor for storing value and then inside another Activity you're using PreferenceManager to retrieve the value. That's why you're getting default value always.

    PreferenceManager is used for storing Preferences for a particular app. They are used for storing app settings where as SharedPreferences are used for saving data or values for working inside the app. They cannot be used for app settings. These are two separate files, though both use the Preferences class.

    You need to use SharedPreferences object the retrieve the value in place of PreferenceManager. you've already created a SharedPreferences object named "preferences" inside your AnotherActivity. Just use this SharedPreferences object to retrieve the data you want to as shown below:

    String value = preferences.getString("key", def_value);
    

提交回复
热议问题