XML string resources vs Java Constant Strings?

后端 未结 5 1117
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 12:56

I have a String is like String data="apps";

i know loading String in Android in two ways..

First one is

so it is a constant i def

相关标签:
5条回答
  • 2020-12-06 12:58

    I'd suggest that you leverage the Android resource system.

    To quote the book Android In Practice: Resources in Android are efficient and fast. XML resources are compiled into a binary format. This makes them friendly at development time, without being slow at Runtime.

    0 讨论(0)
  • 2020-12-06 13:04

    However, speed shouldn't really be an issue in either case. I would recommend organizing based on what makes sense.

    Constants class

    Put strings constants that will be used internally, like database column names or other keys.

    strings.xml

    Put strings that are displayed for the user. This way you can take advantage of localization, etc.

    As per requirement you should be prefer second approach means XML based.

    0 讨论(0)
  • 2020-12-06 13:12

    I think the speed and memory usage would be the same. The constant in a class is just like a constant in the string.xml which is stored once and referenced whenever you need it elsewhere. However, the advantage of storing in string.xml over the other would be the extra import lines of code which would be avoided. Meanwhile, you have to remember that with string.xml storage you can only use it where the context of an activity is accessible.

    0 讨论(0)
  • 2020-12-06 13:16

    You should consider using XML strings as a way to display something to the user and change it depending on locale.

    Never the less, public static final String data="apps"; should be used in order to hide some not-for-user data like db connections, log messages etc.

    0 讨论(0)
  • 2020-12-06 13:24

    The XML string values are meant to be display strings that can be overridden based on locale. This way you can refer to a translatable display value by a constant key.

    0 讨论(0)
提交回复
热议问题