I have an application where I use data persistence with SharedPreference
. From the beginning when I started creating applications, I have always created a class, for example, with name Consts
to store variables of type static final
, in which they do not need to be changed and can be accessed from any part of the project. Here's an example:
public static final String AUTHOR = "author";
When I use SharedPreference
, I usually do it this way:
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Consts.AUTHOR, "Jon Snow");
editor.commit();
Recently watching a video lesson from a Google developer, who works as Android Developer, he used the string resource this way:
<string name="str_author" translatable="false">author</string>
Then in SharedPreference
it did so:
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(getString(R.string.str_author), "Jon Snow");
editor.commit();
These two codes serve the same purpose. But here comes the question that led me to think a little.
- Should I create a string resource or a class of counters?
- Or this option to use string would only be for
SharedPreference
? - In terms of performance and / or practicality, which would be the best option?