What is context in Android?

Context is the element that used most probably in Android Application and get passed frequently. A context in Android is the context of a current state of the application or object. It€ is an entity that represents different environment data. Context helps the current activity to interact with outside the android environments like local files, databases, class loaders associated with the environment, services including system-level services, and many more. In Android, you can get context with different invoking methods: getApplicationContext(), getContext(), getBaseContext().

Today Android training in Chandigarh trainer discus about context in Android. We give the detail of context and important point to use context. Read this article and improve their knowledge.

Context is basically the object that Android runtime to hand over to each = Android application that gets started. It holds all the global information that application required to know – string resources, image resources, Android components in the app (Activity Service, Broadcast Receiver & Content Provider). Sometimes you do a mistake of supposing that there is only one single instance of this class – which will lead to memory leaks in Android app.

context in Android-Android training in Chandigarh

According to Google developer page, Context in Android is an interface to global information about an application environment. This is an abstract class whose execution is delivered by the Android system. It authorizes access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

In Android Development, almost everywhere we can see Context . So we can say it is the most important part of Android development. Everyone must understand to use it correctly.

If we are using wrong Context, it can easily lead to memory leaks in an android application.

It is the actual conditions under which a given artifact or software product is used. It will be used in a usual day to day working conditions. Android Context is very important to carry out usability tests, prototyping sessions, meetings, user studies and other “user-Dependant sessions” in the context of use to get as high ecological validity (see this) of your findings as possible.

You can’t create a new component simply by using ‘new’ since all of these components have their own context. Activity, Service and Application all of these extend from Context.

Context still has some restrictions even it is powerful. It is implemented by ContextImpl, So maximum we can use activity, service and application context in common. But in some conditions like when activity start or pop up a dialog so must use Activity context. since a new Activity is based on another Activity to form a stack, also a pop-up dialogue need to show on top of Activity except for some system alert dialog.

Important points to use context:-

  • When application context is able to handle, do use application context as a first priority.
  • Don’t use Activity which the life cycle is longer than Activity.
  • Do not use a non-static inner class inside activity since the inner class has an implicit reference to the outer class. If using static inner classes, do use weak reference of the objects from the outer class.

Related article: Android developer tools

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

Create your website with WordPress.com
Get started
%d bloggers like this: