3
I have an application where I use data persistence with SharedPreference
. From the beginning when I started creating applications, I always created a class, for example, with name Consts
to store type variables static final
, where they do not need to be changed and can be accessed from any part of the project. See an example:
public static final String AUTHOR = "author";
When I use the SharedPreference
, I usually do it this way:
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Consts.AUTHOR, "Jon Snow");
editor.commit();
Recently watching a certain video lesson from a Google developer, who works as Android Developer, he used the string Resource in this way:
<string name="str_author" translatable="false">author</string>
Then in the SharedPreference
he did so:
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(getString(R.string.str_author), "Jon Snow");
editor.commit();
These two codes have the same purpose. But here comes the question that led me to think a little.
- I must create a string Resource or a class of accountants?
- Or this option to use the string Resource would only be to
SharedPreference
? - In terms of performance and/or practicality, which would be the best option?
I always used count for Sharedpreferences, but I was stunned because the Google guy used string Resource for the situation. He used translatable="false" not to be translated.
– viana
It’s like I said in the reply, I see no advantage in using string Resources as a key to a value stored in Sharedpreferences, unless the value is different by language. In terms of preformance, the string Resources is "transformed" into the value using
getString()
during execution. The constant is "transformed" into the value during compilation.– ramaral
But there is no way to use a variable created in a constant class within XML, unless you instate it first.
– viana
I don’t understand...!
– ramaral
It was just a comment! = D Thanks for the reply.
– viana