Sunday, June 22, 2014

Role of Context in Android


What is Context?

Context is a Interface to global information about an application environment. 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.

It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application).

To understand it better lets see it in this way - 

    • Assume you are visiting a hotel, you would need a room service to order food or cab etc..
    • Here is how we can interpret things
      • You --- Activity
      • Room service --- Context
      • food, cab --- Resources
      • Hotel --- Application
    • This means Context is a way to access resources or do operations or get a global info of the app.
Context base class and child class

Context extends Object

Context is an abstract class, Child classes like Activity, Service etc are derived from it.

Different types of context and ways to get it

You can get the context by invoking 
    • getApplicationContext()
    • getContext()
    • getBaseContext(),
    • this (when in the activity class).

There are two types of context.
  • Activity context is associated with the activity and can be destroyed if the activity is destroyed, it is tied to the lifecycle of Activity. For example, should you launch a new activity, you need to use activity context in its Intent so that the new launching activity is connected to the current activity in terms of activity stack. Also activity context is used in creating views, adapters etc..
TextView tv = new TextView(getContext());
ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
intent=new Intent(this, LoginActivity.class);
startActivity(intent);
  • Application context is associated with the application and will always be same throughout the life of application i.e it is tied to the lifecycle of the Application.
context.getSystemService(LAYOUT_INFLATER_SERVICE)   
getApplicationContext().getSharedPreferences(*name*, *mode*);

Context is used to do below tasks
  1. Loading a resource.
  2. Launching a new activity.
  3. Creating views.
  4. obtaining system service.


When to use getApplicationContext()

getApplicationContext() should be used when you know you need a Context for something that may live longer than any other likely Context you have at your disposal. 

For example use getApplicationContext() when you bind to a Service from an Activity, if you wish to pass the ServiceConnection (i.e., the handle to the binding) between Activity instances via onRetainNonConfigurationInstance(). Android internally tracks bindings via these ServiceConnections and holds references to the Contexts that create the bindings. If you bind from the Activity, then the new Activity instance will have a reference to the ServiceConnection which has an implicit reference to the old Activity, and the old Activity cannot be garbage collected.

Memory leaks due to context

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.







0 comments:

Post a Comment