开发者

What is the best practices in Android, create new string object (static final) or only use the string.xml file (and call many getters)?

开发者 https://www.devze.com 2023-04-09 20:41 出处:网络
In Android there is an conflict about how to get the best performance (like less Garbage Collector).

In Android there is an conflict about how to get the best performance (like less Garbage Collector).

Fir开发者_StackOverflow中文版st, In Android best practices it recommends avoid getters and setters and also avoid creating short lived objects (like: private static final String name = "my name"). Then, what should i do with all strings that I have in my code? Should I set all of them them in the string.xml file and then get it using "this.getString(R.string.myString)" or its still good for permformance keep using "static final String name = "my name" ???

Due to the big amount of strings in my app, if I use only strings from string.xml it would be too many of "getString"s in all code, isn't that bad?


There is a slight performance overhead in calling getString() but no extra memory overhead. And using string.xml will make managing your strings a lot easier. It also makes it much easier to localize these values if you ever choose to do so in the future.

In addition, the values in your resource files will be compiled into final static Strings so it works out similar in the end.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号