What is Context in Android Application?

Save

This is an abstract class whose implementation is provided by the Android system. It allows 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. Context used in android application development everywhere and wrong use of it leads to memory leaks in the application.

 

Application Context Vs Activity Context

They are both the instance of Context class but the application context tied to the lifecycle of application and activity context tied to the lifecycle of activity.

Examples:

  1. If you have to create a singleton object for your application and that object needs a context, always pass the application context.
  2. If you pass the activity context in singleton object which created for the application, 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.

When to use application context in the application starting Service, send a broadcast, loading a resource value. In most cases, use the Context directly available to you from the enclosing component you’re working within.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Harish Gaddam
    Hi, im new to android developer
    i need help
    Application context and Activity Context
    im in confusion after reading this , some what understand but i need to know
    Application context and Activity Context
    where to use
    when to use
    how to use
    at what time to use
    purpose
    • manish choudhary (Moderator)
      Application context bound to application lifecycle and activity context bound to activity lifecycle.
      For example:
      You can use the application context on bound service.
  • css.php

    The results provided were above our expectations both in deliverability timeline as well as operation. They are a top-notch company with many resources and a company we feel honoured to work with again!

    Lee Seward

    Lee Seward

    Founder, Gro-Connect

    Talk to Sales

    Global

    Live Chat
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home