Create a string resource or a class of constants?

3

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?
asked by anonymous 30.03.2017 / 03:51

2 answers

3

For the case you are referring, 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 their formatting, style, pluralization and language capabilities.

Using SharedPreference does not take advantage of any of these features, so I can not find justification to be used there.

Using a primitive type or String, declared as static final , has a much lower cost than using a method to get the resource string.

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

References:

30.03.2017 / 12:46
0

I researched the subject and the result was still a bit mixed, but I will rely on the official documentation.

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

The official documentation suggests (for example) using string resource to SharedPreferences , but I do not believe it's a "truth written in stone" and whether or not you use the resource file to expose data to the user.

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 since it is available from application. Already in the case of constant classes you need to import the class (which would also not be a big problem, since the R class would also be imported). By reading the documentation, I believe the use of the resources is better, as it creates a unique global access ID.

Some caveats:

The resources folder is subject to internationalization (I did not find the page in If you create a file that contains for use in the SharedPrefference, treat it to be "global" ie keep it in the root folder, or treat it by language.

Ack Lay: At the moment I can not do a test to see the result using folders of other languages without using the root, I will test and later I will edit the question with the result.

I hope you have helped.

    
30.03.2017 / 12:54