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?
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.