0

I set up SharedPreferences by following android documentation, requiring me to add each category to a string such as:

<string name="date_of_birth">com.test.activity.dateOfBirth</string>

But since I required a decent amount of variables to be stored and accessed from different activities, I created a class to handle SharedPreferences by following the guide here: https://stackoverflow.com/a/40446531/11019416

Have each variable defined in the SharedPref class as:

public static final String DATE_OF_BIRTH = "com.test.activity.dateOfBirth";

Calling the class in the relevant activity:

SharedPref.init(getApplicationContext());

Writing the necessary data to SharedPreferences:

 String DATE_OF_BIRTH = eDateOfBirth.getText().toString();
                    SharedPref.write(SharedPref.DATE_OF_BIRTH, DATE_OF_BIRTH);

When I test retrieving the data in the next activity by changing a textview field to the DATE_OF_BIRTH variable, I get "com.test.activity.dateOfBirth" as a result instead of the date.

I have tried not using any predefined strings in strings.xml, along with saving the strings as different names, but cannot get it to save correctly.

Am I correctly using strings.xml with this? Or should the content of each string be blank?

jon.bray.eth
  • 527
  • 6
  • 13
  • Have you checked this answer? https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values?noredirect=1&lq=1 – isaaaaame Feb 22 '19 at 20:51
  • @isaaaaame Yes I did, but it didn't clarify the setting of strings in the xml file. I didn't have rep to comment, but I guess my question is do I set the SharedPreferences 'key' name as the name of the string, or the content of the string? – jon.bray.eth Feb 22 '19 at 22:07

0 Answers0