20

I'm helping put together this page: What is a context?

To help illustrate how components are related to a Context, I created this diagram from looking through the framework source code:

Context Subclasses

After digging around a bit, I had the following questions:

  1. What creates a Context and what concrete classes are used? The base Context class itself is an abstract class, which requires almost all of its methods to be implemented. And a ContextWrapper, when instantiated, requires a Context instance to be passed in as it's base context, so there must be at least one concrete Context class.
  2. I understand that the ContextWrapper and its subclasses utilize the wrapper/decorator pattern to add functionality to the base Context class as needed. What is the purpose of the ContextThemeWrapper? It overrides a few methods, but if it's wrapping a base Context, why not just delegate the calls to the base Context? For example, the Activity class extends ContextThemeWrapper, which provides a specific implementation for getTheme() and setTheme(). Why?

The Android developer java docs are a bit vague. E.g., ContextWrapper

orangemako
  • 944
  • 1
  • 11
  • 21
  • I actually was able to step into an app and found the concrete class that `Activity` and `Application` use as the `mBaseContext`. It's `ContextImpl`. https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/ContextImpl.java – orangemako Jul 16 '15 at 18:03

3 Answers3

7

Answering #2:

ContextThemeWrapper adds theming support to a Context, otherwise you can't apply any theme to the Views you create. That's why Activity layouts support themes while widget layouts don't, for example. You can also create a ContextThemeWrapper yourself to override the current theme with another one.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • Sorry for the dumb question but please tell me what is Activity layout and Widget layout? – CopsOnRoad Nov 11 '17 at 13:49
  • Activity layout means a regular XML layout with Views, widget layout means an XML layout containing remote views, used specifically for home screen widgets. – BladeCoder Jan 07 '22 at 10:33
0

Well, I am very late to the party, but The answer to #1 is I believe the ContextImpl class. It is created by the system and then whenever and any class (eg - Activity/Application) calls the attach method which takes in a Context as an argument and internally calls the attachBaseContext of the ContextWrapper class to setup the base context, this ContextImpl is passed as the Context argument. Thus the ContextImpl class provides a solid implementation of the Context class.

Abhriya Roy
  • 1,338
  • 17
  • 23
0

What creates a Context and what concrete classes are used? Context is instantiated by the system using ContextImpl class under the hood. An Activity for example extends ContextThemeWrapper and receives a base context object from outside. When you work with a context in Android usually the last one is represented by a chain of ContextWrappers and a ContextImpl instance at the head. But ContextImpl is an internal class and as an app developer you shouldn't instantiate it manually but treat a Context as an abstraction provided by the system. Maybe you'll find this article useful: Which Context should I use in Android?. What is more important to understand is that a context may be bound to a theme and it leads us to the second question.

What is the purpose of the ContextThemeWrapper? To understand the purpose of the ContextThemeWrapper class it's important to understand what is a theme overlay. In short, ContextThemeWrapper allows to partially override a parent context theme using so called theme overlay. I recommend this article from Android team which describes theme overlays in details. You can also take a look at another SO question: When should one use Theme.AppCompat vs ThemeOverlay.AppCompat?

Valeriy Katkov
  • 33,616
  • 20
  • 100
  • 123