I know I can define some simple type constant (such as String, Integr, Bool, etc. ) either the resource file or const val, such as Code A and Code B, could you tell me which way is the better?
It seems that const val is static variable essentially, sometimes will the Code B cause memory leak ?
For complex variable, the only way is use const val, just like Code C, right?
Code A
<resources>
<integer name="NotificationID">2000</integer>
<string name="NotificationChannelID">ID</string>
</resources>
Code B
const val NotificationID=2000
const val NotificationChannelID="ID"
Code C
const val FLAGS_FULLSCREEN =
View.SYSTEM_UI_FLAG_LOW_PROFILE or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
const val ANIMATION_FAST_MILLIS = 50L
const val ANIMATION_SLOW_MILLIS = 100L
I'd say the only reason to put your constants into resources is need to set different values for different device configurations. For example, different strings for different device locales, different text sizes or margins for different device resolutions.
If it's "realy constant", I don't see any need to put it inside resource. Because it means additional problem like getting value only through context (application context is applicable though). And as Think Twice Code Once mentioned in comment it can lead some problems in testing modules.
And since you asking about memory leaks I think you're not understanding it properly. Holding String, int and other simple constant can't raise any leak because they can't hold any object that should be cleared. But if you hold static Activity, for example, it causes a big leak, as after it finishes it can't be cleared from memory because of reference in that static variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With