-2

Does somebody know where is the getResources() method implemented? It is abstract method in android.content.Context class but I cannot find out which class implements it? I found an answer here on SO but it seems it is not the right answer.

I am searching a particular class which implements getResources() method because I want to know what it exactly does.

Where can I find the implementation which "something does", not only calls something else?

Community
  • 1
  • 1
janzoner
  • 1,420
  • 1
  • 11
  • 19
  • possible duplicate of [Implementation of getResources() Android](http://stackoverflow.com/questions/16922890/implementation-of-getresources-android) – 323go May 29 '14 at 12:36
  • Why does it seem that it's not the right answer? – 323go May 29 '14 at 12:36
  • @323go The accepted answer to the question you mentioned seems to be incorrect. – janzoner May 29 '14 at 12:39
  • Why? What makes you think it's incorrect? – 323go May 29 '14 at 12:42
  • @323go See my [comment](http://stackoverflow.com/a/16923169/3343299) to the accepted answer please. – janzoner May 29 '14 at 12:42
  • I do not understand the downvotes to my answer. It is implemented on `ContextThemeWrapper`. It does call `super` on the method, which takes you to `ContextWrapper` and its implementation. Which uses the `Context` that is passed in to the constructor. – Emmanuel May 29 '14 at 12:50
  • @Emmanuel `ContextWrapper`s implementation is `return mBase.getResources();` and `mBase` is `Context`. It means there is called abstract method too. – janzoner May 29 '14 at 12:53
  • `mBase` is a `Context` that is passed into the constructor. Some class, somewhere implemented all abstract methods of the `Context` class in order to be able to pass it in. Do not forget that `abstract` classes cannot be instantiated and that you can pass into the constructor anything that extends `Context`. – Emmanuel May 29 '14 at 12:57
  • @Emmanuel I understand, but my question is where I can find the implementation which "something does", not only calls something else. – janzoner May 29 '14 at 13:08
  • You will have to go through the docs and see what classes call `ContextWrapper`. I think you are getting confused. `Context` is `abstract` so you need to extend it to be able to instantiate that extended class (not `Context` itself). Since, your class `extends` `Context` you can still use a `Context` variable to refer to it. `Context` is guaranteed to have an implementation of `getResources()`. That is why you see a call like `mBaseContext.getResources()`. `mBaseContext` is a specific implementation of `Context` with has an implementation of `getResources()`. – Emmanuel May 29 '14 at 13:10
  • It is the same as when you can use a `List` variable to refer to an `ArrayList`. The only downside is that `ArrayList` has some extra methods that `List` doesn't require you to implement, but since `ArrayList` confines to the `List` interface, it will provide implementations for the methods declared in `List`. So this syntax is completely valid when you do not need to use the methods provided by `ArrayList` because `List` is enough for your needs: `List list = new ArrayList();`. This is analogous to `Context context = this` if you are inside an `Activity` for example. – Emmanuel May 29 '14 at 13:16
  • Add the relevant parts of the comments to your question. – nhaarman May 29 '14 at 13:19
  • are you referring to me @NiekHaarman? You mean answer? I had an answer but it was downvoted. – Emmanuel May 29 '14 at 13:28
  • @Emmanuel I understand and know everything you posted, but question is clear - I am searching a particular class which implements `getResources()` method because I want to know what it exactly does. – janzoner May 29 '14 at 13:28
  • @Emmanuel No, I'm sorry. I meant janzoner. Instead of asking us to read the comments, it is better to add relevant parts into the question itself to clarify it. – nhaarman May 29 '14 at 13:29
  • 4
    @janzoner Here you go [`ContextImpl`](https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/ContextImpl.java) – Emmanuel May 29 '14 at 13:29

1 Answers1

1

ContextImpl is the canonical Context implementation class. It has many ways for initializing its mResources member variable which can be then accessed with getResources().

Contexts following the usual Activity - ContextThemeWrapper - ContextWrapper inheritance path delegate getResources() to the base context. The base context is set up as ContextImpl instance when the activity is created.

laalto
  • 150,114
  • 66
  • 286
  • 303