2

I have just started learning android programming and I came up with a doubt about the method getResources(). I noticed that when I create a Resources object all I have to do is:
Resources res = getResources();
The first doubt is the following why do I have to do in that way and I mustn't use the java keyword new? Shouldn't I do something like this:
Resources res = new Resources();
The second doubt is the following: at the top of my file I have imported the Resources class.
import android.content.res.Resources;
Now I read the android api and it says that getResources() is a public abstract method, if it is abstract which class implements it? how can I simply call it typing getResources() if it is not declared as static?

zer0uno
  • 7,521
  • 13
  • 57
  • 86

2 Answers2

3

Your activity extends class android.app.Activity which in turn extends class android.content.Context (three levels up the class hierarchy). Class Context declares the abstract method getResources() which means that your activity subclass inherits that method and you can call it from within your onCreate() method (for example).

The method getResources() is declared as abstract in class Context but one of the intermediate classes in the class hierarchy (android.view.ContextThemeWrapper) provides an implementation for the method.

Also that means that creating the Resources object is not your responsibility; it is done by the framework instead.

devconsole
  • 7,875
  • 1
  • 34
  • 42
  • Hi @devconsole. I took a look to the ContextThemeWrapper.java source and I didn't find the implementation of the method `getResources()`. There is only overridden method `getResources()` which calls `super.getResources()`. Do you know about another class which implements `getResources()`? Thanks. – janzoner May 29 '14 at 12:19
  • See also my question [here](http://stackoverflow.com/q/23933397/3343299) if you are interested please. – janzoner May 29 '14 at 13:55
  • @janzoner The point is `ContextThemeWrapper` provides an implementation of `getResources()` which `Activity` inherits. That answers the original question. So what's wrong with my answer? – devconsole May 29 '14 at 21:44
  • @janzoner And yes, it calls `super.getResources()` which calls ContextWrapper's implementation which in turn calls `mBase.getResources()`. I guess it ultimately leads to class `ContextImpl` but that is not a public class. It is an implementation detail of the SDK. See [source code at github](https://github.com/android/platform_frameworks_base/blob/android-sdk-4.4.2_r1/core/java/android/app/ContextImpl.java#L611). – devconsole May 29 '14 at 21:49
  • I am sorry, your answer is ok for the original question. I had my own question in my mind and your answer didn't provide me needed information. But as I said your answer is right. Sorry again. +1 for the answer. – janzoner May 29 '14 at 22:26
1

getResources is actually a method you can access from your Context. So you can really think of this as:

context.getResources()

Your Activity class is your context in this case, which is why you can just call it with the syntax:

getResources()

http://developer.android.com/reference/android/content/Context.html#getResources%28%29

From those docs:

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

Booger
  • 18,579
  • 7
  • 55
  • 72