First of all, let me explain what the context is a bit better, then let's go on to how it can be used and received. Essentially, context is a reference to linking your resources to your program. Each object is given its own context, which contains the resources required to set that object up. It is required for many objects to be created, and to get program identifying information, among other purposes. This makes it invaluable to set up new views and activities, but it can also be used for other purposes. See also this answer for more information.
The context for an item can come from a variety of places. Sometimes it is stored and has to be retrieved, sometimes it is inherited. Basically, this is object oriented programming.
Just to give you a few examples:
Activity inherits context. Thus, if you are in an activity, you only need to pass itself to use the context. It also contains a pointer to getBaseContext()
. You might occasionally need to reference that, if you need the entire application context, but most likely you won't for a while.
View does not inherit context. However, it does have a method getContext(). If you need to get a context from a view, this is the way to get it. This context will not be complete, but will only have the context for the contents of the View.
Fragments also do not inherit context. They contain a method getActivity(), which if the Fragment is active, will return the activity, which is the context for the Fragment.
BroadcastReceivers do not inherit context either. In fact, they do not contain context at all, but simply receive the current context when an event is received (Such as onReceive(Context context, Intent intent))
Context Capabilities
The common actions you can safely take with a given Context object depends on where it came from originally. Below is a table of the common places an application will receive a Context, and in each case what it is useful for:
Original article here.
What i understand by means of context is environment.In simple terms context is the surroundings of anything.So when you are using any form of context you have to decide that what should be the surroundings of the things for which you are using context.
For example if you want some data or field to remain through out the application you should define it in application class.
Now when you get application context in any of your components of your application,this field that you have declared in the application class will be in your context.Hence you can access it.
Same is true for all context type.
If you ever try using alertDialog in the service component by using context "this".Try this one and i bet you will surely get exception as "this" represent environment of service when used in it.And as it is background component we can't add window in that.Hence it will tell you the bad token exception.Which means token generated for surrounding view is not proper for alertDialog to display.
Hope this gives you brief idea what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With