📜 ⬆️ ⬇️

Context in Android application

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, Context is a guide to the system, it can provide resources, access databases, preferences, etc. Also in Android applications there is an Activity . This is similar to the explorer in the environment in which your application runs. The Activity object inherits the Context object. It allows you to access specific resources and information about the application environment.


Context 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 of Context can easily lead to memory leaks in an 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 the whole application), and it can be accessed through the getApplicationContext() function. 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 the scope of the 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 Activity context in this case, it will lead to a memory leak, as the singleton object will retain the reference to the Activity and it will not be destroyed by the garbage collector when it is needed.


In the case when you need to initialize any library in the Activity , always transfer the application context, not the Activity context.


Thus, getApplicationContext() should be used when it is known 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 the Activity and is tied to its life cycle. An Activity Context should be used when you pass a context within an Activity or 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 the getContext() method.


When can I not getApplicationContext ()?



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 reference to the context in an object that lives outside of your Activity or another component, even temporarily, use the reference to the context of the application.


')

Source: https://habr.com/ru/post/421115/


All Articles