76

I noticed that the Activity class has two different methods to get a String resource. This is possible by using:

  • getString(int resId): Return a localized string from the application's package's default string table.

  • getResources().getString(int id): Returns the string value associated with a particular resource ID. It will be stripped of any styled text information.

I don't understand what's the difference between both methods. Can somebody tell me?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Valentin
  • 5,379
  • 7
  • 36
  • 50
  • Well, the main difference is that Activity#getString() will only access your **own** resources, whilst its completely possible to fetch another applications resources as a `Resources?? object and pick from that. I.e. in practical terms, no difference. – Jens Jan 25 '12 at 08:29

3 Answers3

94

They are the same as Activity.getString(int) does exactly that:

 public final String getString(int resId) {
     return getResources().getString(resId);
 }
dacwe
  • 43,066
  • 12
  • 116
  • 140
4

They are the same method, nothing special about them.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
2

In Fragments you can use also getString() instead of getActivity().getString()

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119