Theory : 1 : What is "Context" in Android ?



Simple Example to understand context in android :
Every boss has an assistant to look after, to do all less important and time-consuming tasks. If a file or a cup of coffee is needed, an assistant is on the run. Some bosses barely know what’s going on in the office, so they ask their assistants regarding this too. They do some work themselves but for most other things they need the help of their assistants.
In this scenario,
Boss – is the Android application
Assistant – is context
Files/Cup of coffee – are resources
We generally call context when we need to get information about different parts of our application like Activities, Applications etc.
Some operations(things where the assistant is needed) where context is involved:
  • Loading common resources
  • Creating dynamic views
  • Displaying Toast messages
  • Launching Activities etc.
Different ways of getting context:
getContext()

getBaseContext()

getApplicationContext()

this

A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. An Android app has activities. Context is like a handle to the environment your application is currently running in. The activity object inherits the Context object.



Activity diagram





What's Context exactly?
Per the Android reference documentation, it's an entity that represents various environment data. It provides access to local files, databases, class loaders associated to the environment, services (including system-level services), and more. Throughout this book, and in your day-to-day coding with Android, you'll see the Context passed around frequently.
From the "Android in Practice" book, p. 60.
Several Android APIs require a Context as parameter
If you look through the various Android APIs, you’ll notice that many of them take an android.content.Context object as a parameter. You’ll also see that an Activity or a Service is usually used as a Context. This works because both of these classes extend from Context.

An Android Context is an Interface (in the general sense, not in the Java sense; in Java, Context is actually an abstract class!) that allows access to application specific resources and class and information about application environment.
If your android app was a web app, your context would be something similar to ServletContext (I am not making an exact comparison here).
Your activities and services also extend Context, so they inherit all those methods to access the environment information in which the app is running.

Definition of Context

  • Context represents environment data
  • It provides access to things such as databases

Simpler terms (example 1)

  • Consider Person-X is the CEO of a start-up software company.
  • There is a lead architect present in the company, this lead architect does all the work in the company which involves such as database, UI etc.
  • Now the CEO Hires a new Developer.
  • It is the Architect who tells the responsibility of the newly hired person based on the skills of the new person that whether he will work on Database or UI etc.

Simpler terms (example 2)

  • It's like access of android activity to the app's resource.
  • It's similar to when you visit a hotel, you want breakfast, lunch & dinner in the suitable timings, right?
  • There are many other things you like during the time of stay. How do you get these things?
  • You ask the room-service person to bring these things for you.
  • Here the room-service person is the context considering you are the single activity and the hotel to be your app, finally the breakfast, lunch & dinner have to be the resources.

Things that involve context are:
  1. Loading a resource.
  2. Launching a new activity.
  3. Creating views.
  4. obtaining system service.

Context is the base class for ActivityServiceApplication, etc

Another way to describe this: Consider context as remote of a TV & channel's in the television are resources, services, using intents etc - - - Here remote acts as an access to get access to all the different resources into foreground.
  • So, Remote has access to channels such as resources, services, using intents etc ....
  • Likewise ... Whoever has access to remote naturally has access to all the things such as resources, services, using intents 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.

No comments:

Post a Comment

Featured Post

Lesson 1 : Android Studio : DownLoad And Install and First App

বিসমিল্লাহির রাহমানির রাহীম Lesson 1 : Android Studio : DownLoad And Install  and First App এই লেসনটির ভিডিও ইউটিউবে দেখতে এখানে ক্ল...

Popular Posts