android application

Android Application context

Understanding Context In Android Application

What is Context? 

As the name suggests, it’s the context of the present condition of the android application protest. It lets recently made objects comprehend what has been going on. Commonly you call it to get data with respect to another part of your program.

Context is a handle to the system, it gives organizations like settling resources, getting access to databases. An Android app has activities. It’s like a handle to the condition your application is at currently running in. The activity object acquires the Context question. It enables access to application particular assets and class and data about the application condition.

In a simple way Context objects are so normal and get go around so regularly, it can be anything but difficult to make a circumstance you didn’t expect.  Loading resources, launching another Activity, acquiring a framework benefit, getting inward document ways, and making sees all require a Context (and that is not, in any case, beginning on the full rundown!) to achieve the assignment.

Context types

Not all Context instances are created equal.  The context types generally Depending on the component of Android application, the Context you have the connection to change a little:

The Application Context is the focal interface inside a Spring application for giving design data to the application. … Bean processing techniques for getting to application parts. The capacity to load file resources in a nonspecific design. The capacity to distribute occasions to registered listeners.

Android training institute in Chandigarh provided the quality training and learn all android topics in Android training in Chandigarh. wherever in Android Development Context is the most vital and important thing for an android application, so we should understand to use it correctly.

In a Wrong way use of Context can easily to an android application leaks.

There is a wide range of sorts of context in android, so we should comprehend what are those, how to utilize those and when to use which one.

Application Context

This application context is fixing the process of an getapplicationcontext(). The application context can be used where you require a setting whose lifecycle is separate from the present setting or when you are passing a setting past the extent of an action.

They are both situation of Context, but the application case is fixed to the lifecycle of the application, while the Activity situation is fixed to the circulation of an Activity. Thus, they have access to different information about the application environment.

If you read the article described by Android training in Chandigarh, under the this types of context of get Application Context it notes that you should only use this if you need a context whose process is different from the current context.

Example Use: If you need to make a singleton question for your application and that protest needs a context, always pass the android application context.

In the event that you pass the activity context here, it  will prompt the memory spill as it will keep the reference to the activity and action won\’t be waste gathered.

On the off chance that, when you need to introduce a library in an Activity, always pass the application setting, not the activity context.

Activity Context

The Activity context probably has some data about the present action that is important to finish those calls.

This context is available in an activity. This Is fixing to the lifecycle of an activity. The get Activity Context ()  should be used when you are passing the setting in the degree of an action or you require the setting whose lifecycle is affixed to the present context.

Example Use: If you need to make a protest whose lifecycle is joined to a movement, you can utilize the activity context.

BroadcastReceiver – A broadcast receiver (receiver) is an Android component which allows you to register for system.  A BroadcastReceiver is an Android app component that responds to system-wide broadcast announcements. BroadcastReceiver.onReceive(). Each time a collector forms a communicate, the Context gave to it is another situation.

getContext() in ContentProvider

This context is the application setting and can be utilized like the application context. This can be gotten to by means of getContext() technique.

If the get context() Content Provider is constant local to the caller (i.e. same application process), at that point this will really restore a similar Application singleton. Be that as it may, if the two are in separate processes, this will be a newly created instance representing the package the provider is running in.

When not to use get Application Context() ?

  • It is anything but an entire Context, supporting everything that Activity does. Different things you will attempt to do with this Context will flop, for the most part identified with the GUI.
  • on the off chance that the Context from get Application Context() clutches something made by your approaches it that you don’t clean up. With an Activity, on the off chance that it clutches something, once the Activity gets rubbish gathered, everything else flushes out as well. The Application question stays for the lifetime of your procedure.

Context Capabilities

The common actions you can securely make with a given Context question relies upon where it originated from initially. The following is a table of the regular places an application will get a Context, and for each situation what it is valuable for:

begin an Activity from here, yet it requires that another assignment be made. This may fit particular utilize cases, however can make non-standard back stack practices in your application and is by and is generally not recommended good practice.

This is lawful, but expansion will be finished with the default subject for the framework on which you are running, not what’s characterized in your android application.

The Rule of Thumb

As a rule, utilize the Context specifically accessible to you from the enclosing component you\’re working inside. You can securely expect a reference to remember as long as that reference does not reach out past the lifecycle of that segment. When you have to spare a reference to a Context from a  object that lives beyond your Activity or Service, even briefly, switch that reference you spare over to the application context.

Nothing is more Expensive
than a missed Opportunity