-1

For what purpose Context class is used in android?Please explain me in depth and be more specific .I read all other posts but none of them were specific enough to give me clear understanding.

I know Content class 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.

Like Here,
Intent intent=new Intent(this,new_class.class);

why we are passing Main activity context into the Intent constructor call.what type of information does this activity context contain,how will it help it ,what type of resource access is it providing to it ?(with example please). Similarly, here,

TextView textview=new TextView(this);

Why TextView need activity context?How does it help it.

  • http://stackoverflow.com/a/10641257/793943 please check this ans – Sush Aug 09 '16 at 10:38
  • http://stackoverflow.com/questions/3572463/what-is-context-on-android check this as well. It have some great explanation – MRX Aug 09 '16 at 10:42

2 Answers2

2

There are already several good explanations of Context on Stackoverflow (see the linked questions and us the "search" feature. Also, the source code for Android is available on grepcode.com and you can look yourself if you are really interested. Why trust someone else's answer if you can look yourself? ;-)

However, I will answer your specific questions:

Intent intent=new Intent(this,new_class.class);

why we are passing Main activity context into the Intent constructor call.what type of information does this activity context contain,how will it help it ,what type of resource access is it providing to it ?(with example please).

In this case (the 2-argument constructor for Intent), the Context parameter is only used to determine the package name of the target Activity. Assuming that the package name of your application is "com.example.app", and MyActivity is an `Activity of your application, the following code snippets are all functionally identical:

Intent intent = new Intent(this, MyActivity.class);

Intent intent = new Intent(getApplicationContext(), MyActivity.class);

Intent intent = new Intent();
intent.setComponent(new ComponentName(this, "com.example.app.MyActivity");

Intent intent = new Intent();
intent.setComponent(new ComponentName(this, MyActivity.class);

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app", MyActivity.class);

Intent intent = new Intent();
intent.setClass(this, MyActivity.class);

Intent intent = new Intent();
intent.setClassName("com.example.app", "com.example.app.MyActivity");

Similarly, here,

TextView textview=new TextView(this);

Why TextView need activity context?How does it help it.

All Views need a Context. Think of the Context as the "owner of the View". This controls the lifetime of the View. In general, a View should have the same lifetime as its owning Activity, which is why you usually pass the Activity as the Context parameter when creating a View. When the Activity is destroyed, all of the owned Views are also destroyed. Additionally, the View uses the Context to gain access to resources (drawables, layouts, strings, themes, etc.).

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Seems helpful and thanks for pointing out the error in my answer and clearing my misconception. Just one more doubt, why dont fragments or adapters, etc. have context even though they can control the UI (With parent activity's context) – Mohammed Atif Aug 11 '16 at 10:58
  • And also any link (other than stack overflow answer) will be much appreciated as all the blogs and sites I have seen yet has created the misconception. – Mohammed Atif Aug 11 '16 at 11:05
  • Fragments use the parent activity's `Context`, as you've stated. `Adapter` is an interface anyway. If you look at all the concrete implementations of `Adapter` (ie: `ArrayAdapter`, `CursorAdapter`, `SimpleAdapter`), they all take a `Context` parameter in the constructor (this should be the `Activity Context`, because the adapter's lifetime is linked to the lifetime of the `Activity`. – David Wasser Aug 11 '16 at 12:45
  • I tend to read the Android source code to get my information, so I don't have a ready link for you. You can find the Android source code on `grepcode.com`. What articles and blog posts are you referring to? There's a lot of people out there who (incorrectly) think they know what they are talking about (unfortunately). I'm especially interested in where you got the idea that `Context` has anything to do with threading? – David Wasser Aug 11 '16 at 12:47
1

It act as 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.

You can get all the information from delveloper's official documentation... https://developer.android.com/reference/android/content/Context.html

Moulesh
  • 2,762
  • 1
  • 22
  • 32