How many times android, read to string.xml when a string is obtained?

279 Views Asked by At

If I have this variable activity.getString(R.string.urlService); in my strings.xml in android, and I use this variable 3 times in my code. How many times android read string.xml file? When the android application start the content is upload to memory and is set in the context, and is read once?

1

There are 1 best solutions below

0
On BEST ANSWER
activity.getString(R.string.urlService);

This will get the string 3 times. When i say get the string, i mean a reference to the string that is defined in strings.xml. There is no efficiency benefits here. Infact doing

String myString = activity.getString(R.string.urlService); 

and using myString is more efficient than calling activity.getString 3 times. But this is very very negligible! And the benefits of using strings.xml far outweigh the cons.

Then why use strings.xml?

The beauty of strings.xml is that it makes your life extremely easy when it comes to Localization. Let's say you want your app translated to 10 other languages. All you need to do, is give your strings.xml file to the translator (which google provides by the way for a price), and the translator will give you 10 strings.xml in 10 different languages. You place the english strings.xml in values-en/, the Spanish ones in values-es/ and so on.