This is chapter 4 of our series Mastering Android Context. We will cover types of Context present in Android and which one to use when. If you have not read Chapter 3, we recommend to spend 5 minutes to keep the dots connected.

In chapter 3, we saw both UI and Non-UI Context are almost identical when it comes to communicating with other components, accessing other components and instantiating components. The core difference is UI-Context is short lived and it offers you UI related stuff like theming and window for dialogs.

We know there are many factors in mobile devices. For instance, Configuration changes all the time and locale can change explicitly or implicitly. All of these changes trigger apps to re-create so they can pick the right resources which are best match to current configuraiton. Portrait, Landscape, Tablet, Chinese, German and so on. Your app needs the best possible resources to deliver best user experience. It is the context which is responsible for delivering those best match resources.

Try answering this quesiton. User configuration is portrait and you want to access landscape resources OR user locale is en and you want to access uk resources. How will you do it?

Below are some magical methods from context.

There are many createXXX methods but we are mainly intested in createConfigurationContext. Here is how you can use it.

 Configuration configuration = getResources().getConfiguration();
 configuration.setLocale(your_custom_locale);
 context = createConfigurationContext(configuration);

Here is how you can get hold on any type of Context you desire and when you will call any method on new context you just got, you will get access to resources based on configuration you had set. I know it is amazing. You can send me thank you çard.

Similarily you can create a themed context and use to inflate views with the theme you want.

ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.YOUR_THEME);

Lets discuss more about Activity scope context. Many developers ask this question and I am sure you must have asked this in your Android career.

What is difference between this, getApplicationContext() and getBaseContext()

These are the possible ways you can get hold on Context when you are in Activity scope. this points to Activity itself, our UI-Context and short life context. getApplicationContext() points to yout application instance which is Non-UI and long living context.

Before explaining getBaseContet, let me ask you one question. What is a base class in OOPS? It is the superclass which offers base implementation for some methods.

Similarily baseContext is the base of your activity context which you can set using delegate pattern. You already know you can create Context with any xyz configuration you want. You can combine your this xyz configuration knowledge with base context and your activity will load resources as you desire. Here is the method you can use.

@Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(useYourCustomContext);
    }

Once baseContext is attached, your Activity will delegate calls to this object. If you do not attach activity itself remains baseContext and you get activity when you call getBaseContext.

Now you know the difference and you should feel more confident using any of these methods.

Read Summary here

Subscribe to newsletter and keep learning good stuff

Email

👉 If you like article, click on ♥️ recommend and share on social 👥 media.
👉 Feel free to comment 💬
👉 Follow me 👀 for the more interesting articles. Twitter Medium
👉 Subscribe to newsletter and keep learning