Issue
Wile writing code I found out the that I can the access the string resources via calling the getString()
function on both context
val string = context.getString(...)
and context.resources
val string = context.resources.getString(...)
Which is the right way to do it? Why the two ways?
Solution
Which is the right way to do it?
Either is fine. The implementation of getString()
is:
@NonNull
public final String getString(@StringRes int resId) {
return getResources().getString(resId);
}
(from the source code)
So, they both do the same thing.
Why the two ways?
getString()
is used a lot. Presumably, they added a helper method to Context
to simplify access to string resources. However, while they do that for a couple of resource types, many others are only available via the full Resources
object.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.