Unit - 1 Application Context



Lesson Plan: Unit - 01
Subject: P15A2AAD - Android Application Development
Topic of Study: Application Context
Grade/Level: Master of Computer Applications
Objective: To explore the mechanism of Application Context
Time Allotment: 55 Minutes
  • Context 
    • An Android Context is an Interface (In Java, Context is actually an abstract class) that allows access to application specific resources and class and information about application environment.
    • Context is the base class for Activity, Service, Application, etc
    • Different methods by which you can get context
      • getApplicationContext()
      • getContext()
      • getBaseContext()
      • or this (when in the activity class)
    • Example:
      • TextView TV=new TextView(this);
        • this -> refers to the context of the current activity.
    • Context  is context of current state of the application/object.
    • It allows access to application-specific resources and classes.
    • It is use for application-level operations such as launching activities, broadcasting and receiving intents, loading resources, launching a new Activity, obtaining a system service, getting internal file paths etc. 
    • Context used in android application development everywhere.
    • So, take care the wrong use of it leads to memory leaks in the application.
    • Ways to get Context:
      • view.getContext():
        • It's the activity context which the view is hosted.
      • Activity.getApplicationContext():
        • Exa.: Context con = getApplicationContext();
        • get the current application context, when we need context, this global context need to be consider at first.
        • Toast can use ApplicationContext.
  • Types of Context...
    • Activity/Service Context 
      • Each Activity or Service, and their corresponding base context are unique per-instance.
      • It is refers to the application level environment.
      • This context is available in an activity / service. 
      • This context is tied to the life cycle of an activity or a service. 
      • Example: If you have to create an object whose life cycle is attached to an activity / service, you can use the activity/service context.
    • Application Context 
      • Application – is a singleton instance running in your application process.  
      • It can be accessed via methods like getApplication() from an Activity or Service, and getApplicationContext() from any other object that inherits from Context.  
  • Difference between getApplication() and getApplicationContext()
    • Android OS has main four components including Activity, Service, Content Provider and Broadcast receiver.
    • Actually both functions return application object since application itself is a context. 
    • So why android provide two functions? 
      • The reason is because getApplication() is only able to be used in Activity and Service. 
      • In other components like BroadcastReceiver is only able to use getApplicationContext() to get application object.

1 Comments

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form