-3

May I ask what's the different between

getResources().getString(R.string.my_string);

and

getString(R.string.my_string);

and also just

R.string.my_string;

I know that both return different value

for example:

textView.setText(getResources().getString(R.string.my_string));
textView.setText(getString(R.string.my_string));
textView.setText(R.string.my_string);

I have try this three ways

all works

but I wonder to know why

Killua_J
  • 9
  • 2
  • Possible duplicate of [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – Xavi Jimenez Aug 01 '19 at 05:50

2 Answers2

1

No difference, you can read the internal files. /** * Return a localized string from the application's package's * default string table. * * @param resId Resource id for the string */ @NonNull public final String getString(@StringRes int resId) { return getResources().getString(resId); }

lazy
  • 149
  • 8
0

Hi The Android resource system keeps track of all non-code assets associated with an application. You can use this class to access your application's resources. You can generally acquire the Resources instance associated with your application with getResources().

practically there is no difference in getString(R.string.your_string) and getResources().getString(R.string.my_string); but I will suggest you to always use getResources().getString(R.string.my_string); for calling the resources.

R.string.my_string; will return the id(int) of the my_string. So this work like this getResources() is parent class contain all the resources of application by getString() you are informing the processor that your resource type is string and by R.string.my_string you give the id of your string. Hope you understand.

Virendra Varma
  • 895
  • 1
  • 12
  • 23