Understanding “Context” In Android Application

What is Context in Android?

If we go by the name, it’s the context of the current state of the application (object). It lets newly-created objects understand what has been going on. You can use the context to get information regarding activity and application.

And also, Context is like a handle to the system, it provides access to the resources, databases and preferences, and etc. An Android app has activities. It’s like a handle to the environment your application is currently running in. The activity object extends the Context object. It allows access to application specific resources and class and information about the application environment.

Context is almost everywhere in Android Development and it is the most important thing in the Android Development, so we must understand to use it correctly.

Wrong use of Context can easily lead to memory leaks in an android application.

There are many different types of context in android, so let’s understand what are those, how to use those and when to use which one.

Application Context

It is an instance which is the singleton and can be accessed in an activity via getApplicationContext(). This context is tied to the lifecycle of an application. The application context can be used where you need a context whose lifecycle is separate from the current context or when you are passing a context beyond the scope of an activity.

Example Use: If you have to create a singleton object for your application and that object needs a context, always pass the application context.

If you pass the activity context here, it will lead to the memory leak as it will keep the reference to the activity and activity will not be garbage collected.

In case, when you have to initialize a library in an activity, always pass the application context, not the activity context.

You only use getApplicationContext() when you know you need a Context for something that may live longer than any other likely Contextyou have at your disposal.

Activity Context

This context is available in an activity. This context is tied to the lifecycle of an activity. The activity context should be used when you are passing the context in the scope of an activity or you need the context whose lifecycle is attached to the current context.

Example Use: If you have to create an object whose lifecycle is attached to an activity, you can use the activity context.

getContext() in ContentProvider

This context is the application context and can be used similar to the application context. This can be accessed via getContext() method.

When not to use getApplicationContext() ?

  • It’s not a complete Context, supporting everything that Activitydoes. Various things you will try to do with this Context will fail, mostly related to the GUI.
  • It can create memory leaks if the Context from getApplicationContext() holds onto something created by your calls on it that you don’t clean up. With an Activity, if it holds onto something, once the Activity gets garbage collected, everything else flushes out too. The Application object remains for the lifetime of your process.

The Rule of Thumb

In most cases, use the Context directly available to you from the enclosing component you’re working within. You can safely hold a reference to it as long as that reference does not extend beyond the lifecycle of that component. As soon as you need to save a reference to a Context from an object that lives beyond your Activity or Service, even temporarily, switch that reference you save over to the application context.

Smart Mobile devices make communication so much simpler. Having a mobile application for your business is the first step to allowing smooth communication between you and your customers. We create/produce applications that cater for iOS and Android supported mobiles be it a tablet or a mobile phone we will be able to cater for you. Visit www.whereisthebeef.co.za for more information.

3 Responses
  1. I just want to tell you that I’m new to blogging and honestly savored you’re web site. Most likely I’m going to bookmark your blog . You surely come with incredible stories. Kudos for sharing with us your web site.

  2. I as well as my pals have already been taking note of the great hints found on your web blog and the sudden I had a terrible suspicion I had not expressed respect to the site owner for them. Those ladies were definitely totally glad to see them and have now in fact been enjoying them. We appreciate you simply being quite helpful and for getting some good topics most people are really desperate to know about. Our own honest apologies for not expressing appreciation to you earlier.

Leave a Reply