Which Context should I use?

The code in this directory references different types of contexts. Please read about what each represents before deciding which one you should use.

Embedder's Activity Context

The fragment that WebLayer is loaded in holds a reference to the activity that it is currently attached to. This is what's referred to by mEmbedderActivityContext in BrowserImpl and BrowserFragmentImpl. It should be used to reference anything associated with the activity. For instance, embedder-specific resources, like Color resources which are resolved according to the theme of the embedding activity.

WebLayer's Activity Context

WebLayer has a lot of resources of its own which need to be accessed by the implementation code. We thus wrap the embedder‘s activity context so that resource and assert look-ups against the wrapped context go to the WebView or WebLayer support APK and not the embedder’s APK. This wrapped Context is what's returned by BrowserImpl.getContext(). Use this when referencing WebLayer specific resources. This is expected to be the most common use case.

Embedder's Application Context

Occasionally, we need the embedder‘s application context, as opposed to its activity context. For instance, fetching the current locale which applies to the entire application. Similar to WebLayer’s Activity Context, this context is also wrapped in our implementation so we can reference WebLayer-specific resources. This is what‘s returned by ContextUtils.getApplicationContext(). It shouldn’t be downcast to Application (or any subclass thereof) since it's wrapped in a ContextWrapper.