Application Context, Activity Context and Memory leaks

What is context?

In simple words I can say we need to tell android that on which Activity you want to do something.

For example, you want to print a toast message you need to give him context of that activity to perform correctly.

So, we know what context is. It is a reference of current state of app.

Context is probably most used element in making of android application, but when to use which context is important to know.

I am beginner in android and love to go through different codes, I was just skimming through one github code and that developer passed context and Activity and that kept me thinking what the difference is between these two.

today I want to share my understanding of what is the difference between two types of contexts in Android.

1. Application context

2. Activity context

And when I researched on it, I felt that no one tells this to beginners, rookies kept making these kinds of mistakes because of not proper tutorials available from they can understand all topics in depth.

We pass different types of contexts but not all context instances are created equally.

When you search on Google that what is the difference between application context and activity context

You will probably get this, they both are instances of Context, but Application context is tied to lifecycle of Application and activity context is tied to lifecycle of Activity.

But exactly what is the difference? not every beginner is able to know these things.

Overall, I should tell you usually on android framework methods where context is expected, it makes no difference which one you pass, but you should be always aware of memory leaks, What are memory leaks? We will get to that later. For instance, if you pass getApplicationContext() or MainActivity.this for displaying Toast, it will work with both but it not proper practice to pass any context.

Context is a handle to system. What is means is context contains all important data like Environment (local files, databases), system services, location services etc. if you pass getApplicationContext() for only displaying Toast it’ll take up so much memory. For displaying Toast message, one should pass activity context because we have no purpose of that Toast out of that activity.

When activity is destroyed that context also be destroyed with it and it prevents memory leaks.

And we must pass activity context because activity context has some more information about current activity and sometimes that is necessary to complete those requests. Same happens with navigating to new activity person can pass any context but with application context it can create nonstandard behaviours in application and it is not considered as a good practice.

So, how to figure out when to use which context??

I found out one image on stackoverflow and it generally covers all aspects.

We can say that from looking at table that for UI related components application context is not suitable.

In general, we should use activity context unless we have a good reason not to, in most cases we should use the context which is directly available. You can switch Activity context to application context when you need to save reference to out of activity lifecycle.

Now, what are memory leaks??

Memory leaks occur when an application allocates memory for object but fails to release the memory when object is no longer being used. Memory leaks can happen in any program, but it is most important to understand in android because less knowledge of Activity lifecycle, storing variables etc.

For example, memory leak happens when user uses static string variable even if one does not need it outside of that activity. Same thing with Cursor, user passes query in cursor but because it is completing his/ her task they forgot to close it after using cursor. And it allocates memory every time when one asks for data from cursor, and it will result in memory shortage in application. Over time, leaked memory accumulates and results in poor app performance and even crashes.

Currently pursuing B.E. in IT and learning Android development