Context in Android Application, What, When and Why

Ahmed Yehya
windroid
Published in
3 min readMay 29, 2017

Context refers to the status, behavior, or process of an operation. If we apply that meaning to the parts of an application, we will define it as follows:
Is the behavior of the currently running part of the application, whether it is Object or otherwise, and it is a combination or entity that represents the working environment of the different data types. Context helps Activity, which is currently interacting with the other side of the Android system, such as local files, databases, the part for bringing Android classics (ready-made classes), services, and many more.

The meaning of the context is simply to deal with the system to manage the various processes within the application.

It is certain that you have passed the term Context while working on your applications and what you want to realize is that the context in Android is very important as you understand how to handle it so I will show you the right way to use it, The faulty memory of this object may lead to memory depletion in the application (Memory leaks)

There are several forms of context in Android that we will recognize, how to use them, and when to use one.

Application Context

This context belongs to the Lifecycle of an application. The Application context can be used if you want a context that is separate from the Activity context that is currently running or when you want to pass the Context to a range beyond the currently active activity It is called by

getApplicationContext()

Example of use: If you want to create an object that works alone and is not specific to a specific activity and this object needs a Context, always pass the Application Context.

Example of clarification: If you want to create a new class for a particular view that you want to create and you want to call Object of this class in more than one activity in this case you will pass it an Application Context.

If you pass Activity context it will cause Memory leak because in this case it will be the reference to Activity and the Activity will not be collected when it finishes its work in the Garbage collection. Also, if you want to call a specific library within the Activity, use the Application context.

Activity Context

This context works within the activity and is associated with the Lifecycle of an activity and is used if you see the Context pass in the scope of the activity only or if you want the Lifecycle of context to belong to the Lifecycle of current context,

Example of use: If you want to create an object and its Lifecycle is dependent on the activity within it and not related to other activities

getContext() in Content Provider

This context is a form of Application Context and can be used the same use and is often used with the Content Provider and is accessed by

getContext()

When do not we use getApplicationContext()?

The Application Context is not, as some developers understand, a full context that supports all of the activity, Some of the things you are trying to use with Context will not work, especially those related to the GUI.

Fixed base Apply it to use Context

  • In most cases, use the context that is available to you in the element you are working on.
    -Reference Reference or the scope within which the element works. If only at the Activity level, use the Context. If at the Application level, use the Application Context so that the Lifecycle does not exceed the Lifecycle you want to use.
    -If you want to use a specific object after the Lifecycle of activity is finished, use the Application Context.

--

--