Context in Android application

Original author: Amit Shekhar
  • Transfer

Context in Android application


What is Context?


As the name suggests, this is the context of the current state of the application or object. This allows newly created objects to understand what is happening. It is usually called to get information about another part of the program.


In addition, it Contextis a conductor to the system, it can provide resources, get access to databases, preferences, etc. More in Android applications there Activity. This is similar to the explorer in the environment in which your application runs. An object Activityinherits an object Context. It allows you to access specific resources and information about the application environment.


Context is present almost everywhere in the Android application and is the most important part of it, so you need to understand how to use it correctly.


Incorrect use Contextcan easily lead to memory leaks in the Android application.


There are many different types of context, so let's see what each of them is, how and when to use them correctly.


Application context


This is a singleton instance (the only application for all), and it can be accessed through a function getApplicationContext(). This context is tied to the application life cycle. An application context can be used where you need a context whose life cycle is not related to the current context or when you need to transfer the context beyond Activity.


For example, if you need to create a singleton object for your application, and this object needs some kind of context, always use the application context.


If you pass the context Activityin this case, it will lead to a memory leak, as the singleton object will save the reference to Activityand it will not be destroyed by the garbage collector when it is needed.


When you need to initialize a library in Activity, always pass the application context, not the context Activity.


Thus, getApplicationContext()you need to use when you know that you need a context for something that can live longer than any other context that you have.


Activity Context


This context is available in Activityand tied to its life cycle. The context Activityshould be used when you pass the context within the framework Activityor you need a context whose life cycle is tied to the current context.


getContext () in ContentProvider


This context is the application context and can be used similarly to the application context. It can be accessed through a method getContext().


When can I not getApplicationContext ()?


  • This is not a complete context that supports everything that it can Activity. Some things you try to do with this context will fail, mainly related to the GUI.
  • Memory leaks may occur if the context from is getApplicationContext()held on an object that you do not subsequently clean. If the context is held somewhere Activity, then as soon as it is Activitydestroyed by the garbage collector, everything else is also destroyed. The object Applicationremains for the whole life of your process.

Rule of thumb


In most cases, use the context available directly from the component in which you are currently working. You can safely keep a link to it, if it does not go beyond the life cycle of this component. As soon as you need to keep a context reference in an object that lives outside of your Activityor another component, even temporarily, use the reference to the application context.


Also popular now: