While I'm developing Android-Apps I like to have a look at internal SDK implementations. It's a huge Framework and sometimes it helps a lot if you know, how internal Methods are implemented.
Now the last source-check I have done is really confusing to me. I looked at Context class to read some implementations. Unfortunately most of them are abstract, that's why Android invents the ContextWrapper, (a simple Adapter Pattern).
The problem is, that I couldn't find some Methods I'm interested in. Lets take getResources as Example. I have a ContextObject and call getResources on it. This Context is an instance of Activity (which is not implementing getResources() by itself).
Same for ContextThemeWrapper which is the direct parent-class of Activity. The ContextWrapper then invokes getResources() on its Context member. But who is implementing it then?
EDIT: added Snippet from ContextWrapper
public class ContextWrapper{
Context mContext
public ContextWrapper(Context ctx){
mContext = ctx
}
public Resourced getResources(){
return mContext.getResources()
}
//... all other Methods are implementing the same AdapterPattern
}
so the question can also be "which Context is passed to ContextWrapper, which is implementing required methods"
I finally found it out myself with the helpful tip from nicholas.hausschild, to just to run the debugger.
Unfortunatly this seems to be the only way, to find that out, because it's implemented in a Class called ContextImpl, which is package-readable (though not documented). You have to install Eclipse source-code plugin (which seems to be a little buggy to install, if you had downloaded and linked the Android-SDK to eclipse already) to see the sources.
For anyone who is interested in these sources: clickme
The getResources() method on the Context class is abstract, meaning whatever inherits from it needs to provide or override its implementation. The implementation of getResources() on the ContextWrapper class is concrete, and is where you should find its code.
Also, you have your inheritance chain a little backwards. Activity is a subclass of ContextThemeWrapper, not the other way around:
java.lang.Object
↳ android.content.Context // abstract Resources getResources()
↳ android.content.ContextWrapper // Resources getResources()
↳ android.view.ContextThemeWrapper // inherits ContextWrapper.getResources()
↳ android.app.Activity // inherits ContextWrapper.getResources()
The Android Developer Reference will help you with the inheritance structures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With