Create a Resource string or a class of counters?

Asked

Viewed 173 times

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?

2 answers

3


For the case you refer to, key to a value stored in Sharedpreferences, you must use constants.

The Resource string is intended for text to be presented to the user, taking advantage of its formatting, style, pluralization and language capabilities.

The use in Sharedpreference does not take advantage of any of these characteristics, so I can find no justification(1) to be used there.

The use of a primitive type or String, declared as static final, has a much lower cost than using a method to get the Resources string.

(1) - Unless you have saved different values per language and want to access them by a single key.

References:

  • 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.

  • 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.

  • But there is no way to use a variable created in a constant class within XML, unless you instate it first.

  • I don’t understand...!

  • 1

    It was just a comment! = D Thanks for the reply.

0

I have researched the subject and the result has been somewhat mixed, but I will draw on the official documentation.

"Should I create a Resource string or a class of counters?"

To official documentation suggests (for example) using string Resource for Sharedpreferences, however I do not believe that it is a "truth written in stone" and whether or not you use the Source file to expose data to the user.

"In terms of performance and/or practicality, which would be the best option?"

When the performance does not believe that it is something that manages delays in the application, however, the practicality of using string Resource is very large as it is available from the entire application. In the case of constant classes you need to import the class (which also wouldn’t be a big problem, since the class R would also be imported). Reading the documentation, I believe it is best to use Resources, as it creates a unique global access ID.

Some caveats:

The folder of resources is a likely internationalization (did not find the page in PT-BR), if you create a file that contains for use in Sharedprefference, treat it to be "global" ie keep it in the root folder, or treat it by language.

Ack Lay: Currently I cannot do a test to see the result using other language folders without using the root, I will do the test and then edit the question with the result.

I hope it helped.

  • Thank you for the reply Carlos! As for "The Resources folder is an internationalizing [...]" for this case does not make much difference, so the translatable="false" so that it is not translated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.