Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to explain context in Android to a friend

Tags:

android

I am trying to explain context to a friend. Context as written in the documentation is that it is interface to the system resources (sensors, vibrator etc) but would this mean memory, CPU etc?

And also, is context a reference to an activity? I mean is it possible to compare that a context is equal to a uiviewcontroller in iOS programming and application context is the app delegate? I am still learning Android so I might not be the best to make an answer. If any one can provide a thorough answer on this I would be very thankful.

Regards not a native English speaker so I would also like to know what context means in programming.

like image 814
user676842 Avatar asked Mar 25 '11 14:03

user676842


People also ask

What is the meaning of context in Android?

Definition. it's the context of current state of the application/object. 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 and package/application).

What should I pass in context Android?

It is used to return the Context which is linked to the Application which holds all activities running inside it. When we call a method or a constructor, we often have to pass a Context and often we use “this” to pass the activity Context or “getApplicationContext” to pass the application Context.

What is context in app?

In Android it a context simply means the context of the current state of the application or object. Generally, we use it to get information regarding another part of your program (activity/application package). There are few keypoint to understand about context : It is the current state of the app.

What is context in Android Dev?

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.


2 Answers

I am trying to explain context to a friend. Context as written in the documentation is that it is interface to the system resources (sensors, vibrator etc) but would this mean memory, CPU etc?

No, "interface" to memory and CPU are implicitly provided by the execution and memory model of Java language.

And also, is context a reference to an activity?

Activities are a kind of context. You can get a full list of classes that inherit from Context in the docs under "Indirect Subclasses".

I mean is it possible to compare that a context is equal to a uiviewcontroller in iOS programming and application context is the app delegate? I am still learning Android so I might not be the best to make an answer. If any one can provide a thorough answer on this I would be very thankful.

I think the two are quite different. You might check out Tasks and the Back Stack to see more about Activities as loosely-coupled, separate units. A subclass of Application in your app may be similar to an app delegate in iOS.

Regards not a native English speaker so I would also like to know what context means in programming.

The English definition might help:

2 : the situation in which something happens : the group of conditions that exist where and when something happens

In programming "context" is quite close to this definition. The context often tells a function or object the answers to things like:

  • Where am I?
  • Where is resource X?
  • Is feature Y available?
  • What was I just doing? (especially common in the case of C)

Honestly, I think you can get very far in Android programming without understanding Context.

like image 177
Matthew Willis Avatar answered Oct 13 '22 07:10

Matthew Willis


Context in android is a basic interface to access several things:

  1. System services. Vibrator, sensors and so, as you already mentioned.
  2. Resources(strings.xml, shared preferences and so on).
  3. Views, belonging to the context.

Context can be described as a programming environment for your code. It is a context in which your code executes.

Context can not be linked just to an activity, Application, Dialog, Service and others also implements it.

Context in this particular situation can be represented as a cloud of objects and things you can access, which are visible to you. From activity you can access views, services and resources. In application context there are no view, but still you see resources and services. And so on.

like image 41
Vladimir Ivanov Avatar answered Oct 13 '22 08:10

Vladimir Ivanov