Learn about Context in Android apps

Tram Ho

1. What is Context?

Context in Android is the context of where we are present and interact.

Some views on context:

  • It is the current context of the application.
  • It can be used to get information related to activity and application.
  • It can be used to access application resources, databases, etc.
  • Both Activity and Application are inherited from Context class.
  • Context is used almost everywhere in Android application development and it’s the most important thing in Android application development, so we have to understand in order to use it correctly.

Using the wrong context can lead to a memory leak in an Android app.

Since there are different types of contexts in Android, we often have to choose which context to use in which location accordingly. So, we have to understand each type of context, how to use them, and when to use which one accordingly.

There are mainly two types of contexts:

Application Context: It is the context of the application and we will always be in the application. For example – MyApplication (extending Application class). The Context here is an instance of the MyApplication class. Activity Context: It is the context of Activity. With each different activity we will have a different context. Example – MainActivity. The context here is an instance of MainActivity. If you’re preparing for the next Android Interview, take our Android Professional Course to learn the latest on Android and find jobs at top tech companies.

1.1 Aplication Context

It is an instance, singleton, and can be accessed within the activity through the getApplicationContext() . The Application Context is tied to the life cycle of an application. Application context can be used when you need a context with its life cycle separate from the current context or when you are moving a context beyond the scope of the activity.

For example, if you have to create a singleton object for your application and that object needs a context, always use the Application Context.

If you use the Activity Context here, it will lead to a memory leak as it will keep references to the activity and the activity will not be tidy up by the GC.

In case, when you have to instantiate a library in an activity, use application context instead of activity context.

Use getApplicationContext () when you need a context for something that can outlive any other.

1.2 Activity Context

Each activity will have its own context. This context is associated with the life cycle of the activity. The activity context should be used when you are in the scope of the activity or you need the context whose life cycle is tied to the current context.

Example: If you have to create an object whose lifecycle is tied to an activity, you could use the activity context.

The application hierarchy looks like this:

In the above description, we have MyApplication [Here we have Application context] [It’s the closest context] MainActivity1 [Here we have both the context of activity (MainActivity1) and the context of the application (MyApplication)] [Language The closest context is Activity context] MainActivity1 [Here we have both the activity context (MainActivity2) and the application context (MyApplication)] [The closest context is the Activity context] ** getContext () in the ContentProvider ** This Context is application context and can be used as an application context. You can get it with the getContext () .

1.3 So how to use context appropriately?

Let’s learn how to use context using the following example:

Let’s say we have MyApplication class (inheriting from Application class). And, another MyDB class is Singleton. And MyDB needs to be provided with a context to be able to do the job. What context will we use in this case?

The answer is Application Context, because if we use the Acitivity Context (e.g. MainActivity1), even if MainActivity1 is not in use, MyDB will keep the unnecessary reference, leading to memory leak.

So always remember, in the case of Singleton (lifecycle attached to application lifecycle) always use Application Context.

So when to use the Activity Context. Whenever you are in activity, for any UI interaction like data display, dialog box etc., use the Activity Context.

Always try to use the closest available to you. When you are in Activity, the closest context is the Activity context. When you are in an Application, the closest context is the Application Context. If Singleton, use Application Contex.

summary

Reference: https://blog.mindorks.com/understanding-context-in-android-application-330913e32514

Share the news now

Source : Viblo