What is 'Context' on Android?

Multi tool use
Multi tool use












1688















In Android programming, what exactly is a Context class and what is it used for?



I read about it on the developer site, but I am unable to understand it clearly.










share|improve this question




















  • 14





    Related to this question

    – Christopher Perry
    Jan 25 '13 at 6:41
















1688















In Android programming, what exactly is a Context class and what is it used for?



I read about it on the developer site, but I am unable to understand it clearly.










share|improve this question




















  • 14





    Related to this question

    – Christopher Perry
    Jan 25 '13 at 6:41














1688












1688








1688


607






In Android programming, what exactly is a Context class and what is it used for?



I read about it on the developer site, but I am unable to understand it clearly.










share|improve this question
















In Android programming, what exactly is a Context class and what is it used for?



I read about it on the developer site, but I am unable to understand it clearly.







android android-context






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 4 '18 at 11:11









naXa

13.5k889136




13.5k889136










asked Aug 26 '10 at 6:12









BrigadierBrigadier

8,7854139




8,7854139








  • 14





    Related to this question

    – Christopher Perry
    Jan 25 '13 at 6:41














  • 14





    Related to this question

    – Christopher Perry
    Jan 25 '13 at 6:41








14




14





Related to this question

– Christopher Perry
Jan 25 '13 at 6:41





Related to this question

– Christopher Perry
Jan 25 '13 at 6:41












28 Answers
28






active

oldest

votes


















1321














Putting it simply:



As the name suggests, 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).



You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in a class that extends from Context, such as the Application, Activity, Service and IntentService classes).



Typical uses of context:





  • Creating new objects:
    Creating new views, adapters, listeners:



    TextView tv = new TextView(getContext());
    ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);



  • Accessing standard common resources:
    Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:



    context.getSystemService(LAYOUT_INFLATER_SERVICE)
    getApplicationContext().getSharedPreferences(*name*, *mode*);



  • Accessing components implicitly:
    Regarding content providers, broadcasts, intent



    getApplicationContext().getContentResolver().query(uri, ...);







share|improve this answer





















  • 45





    In your example context.getSystemService(LAYOUT_INFLATER_SERVICE), where and how is context defined?

    – Dennis
    Dec 4 '12 at 21:47






  • 4





    It is well explained why we need context while dynamically creating a text view. But while dynamically creating arrays of text views we don't have to mention any context. Why is that so ? TextView textview = new TextView[10];

    – Abhinav Arora
    Dec 26 '14 at 8:09






  • 26





    @AbhinavArora when defining the array, you're not actually constructing an instance of any text views (you're basically just creating the space for them to go into). At the point you come to put any values into that array, you'll either need pre-created TextViews, or need a Context to allow you to create them.

    – mc1arke
    Mar 18 '15 at 7:57






  • 17





    Why do views need context? For example, what would TextView not be able to do if it didn't have the context?

    – dinosaur
    Apr 22 '16 at 17:33






  • 7





    A piece of code without a "Context" can run on each operating systems that has JVM. But if there is a context it should run on Android.If you want to use Android specific things(accessing device location, taking a photo, running a background service etc.) you need a context Although you don't need if you make an http request. Context can be assumed as a bridge between Java and Android.

    – Faruk Toptas
    Jun 16 '16 at 12:14





















444














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 Activity, Service, Application, 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.






share|improve this answer





















  • 3





    Ok, so the class derived from the Activity IS a context itself. That is why by passing this to the newly created views, we pass the context.

    – Oleg
    Jan 25 '15 at 14:40






  • 4





    I wonder if it's a good design decision to have the context accessible from so many different places? One static getContext() in the application would have been enough in my opinion.

    – Trilarion
    Dec 28 '15 at 9:05











  • @Trilarion... It depends on how you want to use context getApplicationContext(), getContext(),getBaseContext() ..... Refer this - > (stackoverflow.com/a/10641257)

    – Devrath
    Aug 14 '16 at 15:00











  • Just to expand the last piece with creating a textview: In some cases it may be necessary to call SomeActivityName.this. IN a thread for an instance, this refers to teh thread and not the activity

    – Zoe
    May 14 '17 at 10:37











  • Is context object unique for an apk or Android OS? Can an application have two different contexts?

    – valijon
    Aug 30 '18 at 12:14



















281














Source





The topic of Context in Android seems to be confusing to many. People just know that Context is needed quite often to do basic things in Android. People sometimes panic because they try to do perform some operation that requires the Context and they don’t know how to “get” the right Context. I’m going to try to demystify the idea of Context in Android. A full treatment of the issue is beyond the scope of this post, but I’ll try to give a general overview so that you have a sense of what Context is and how to use it. To understand what Context is, let’s take a look at the source code:



https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/Context.java



What exactly is Context?



Well, the documentation itself provides a rather straightforward explanation: The Context class is an “Interface to global information about an application environment".



The Context class itself is declared as abstract class, whose implementation is provided by the Android OS. The documentation further provides that Context “…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 understand very well, now, why the name is Context. It’s because it’s just that. The Context provides the link or hook, if you will, for an Activity, Service, or any other component, thereby linking it to the system, enabling access to the global application environment.
In other words: the Context provides the answer to the components question of “where the hell am I in relation to app generally and how do I access/communicate with the rest of the app?” If this all seems a bit confusing, a quick look at the methods exposed by the Context class provides some further clues about its true nature.



Here’s a random sampling of those methods:





  1. getAssets()

  2. getResources()

  3. getPackageManager()

  4. getString()

  5. getSharedPrefsFile()


What do all these methods have in common? They all enable whoever has access to the Context to be able to access application-wide resources.



Context, in other words, hooks the component that has a reference to it to the rest of application environment. The assets (think ’/assets’ folder in your project), for example, are available across the application, provided that an Activity, Service or whatever knows how to access those resources.
Same goes for getResources() which allows to do things like getResources().getColor() which will hook you into the colors.xml resource (nevermind that aapt enables access to resources via java code, that’s a separate issue).



The upshot is that Context is what enables access to system resources and its what hooks components into the “greater app".
Let’s look at the subclasses of Context, the classes that provide the implementation of the abstract Context class.
The most obvious class is the Activity class. Activity inherits from ContextThemeWrapper, which inherits from ContextWrapper, which inherits from Context itself.
Those classes are useful to look at to understand things at a deeper level, but for now it’s sufficient to know that ContextThemeWrapper and ContextWrapper are pretty much what they sound like.
They implement the abstract elements of the Context class itself by “wrapping” a context (the actual context) and delegating those functions to that context.
An example is helpful - in the ContextWrapper class, the abstract method getAssets from the Context class is implemented as follows:



@Override
public AssetManager getAssets() {
return mBase.getAssets();
}


mBase is simply a field set by the constructor to a specific context.
So a context is wrapped and the ContextWrapper delegates its implementation of the getAssets method to that context. Let’s get back to examining the Activity class which ultimately inherits from Context to see how this all works.



You probably know what an Activity is, but to review - it’s basically 'a single thing the user can do. It takes care of providing a window in which to place the UI that the user interacts with'.
Developers familiar with other APIs and even non-developers might think of it vernacularly as a “screen.” That’s technically inaccurate, but it doesn’t matter for our purposes. So how do Activity and Context interact and what exactly is going in their inheritance relationship?



Again, it’s helpful to look at specific examples. We all know how to launch Activities. Provided you have “the context” from which you are you are starting the Activity, you simply call startActivity(intent), where the Intent describes the context from which you are starting an Activity and the Activity you’d like to start. This is the familiar startActivity(this, SomeOtherActivity.class).



And what is this? this is your Activity because the Activity class inherits from Context. The full scoop is like this: When you call startActivity, ultimately the Activity class executes something like this:



Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode);


So it utilizes the execStartActivity from the Instrumentation class (actually from an inner class in Instrumentation called ActivityResult).



At this point, we are beginning to get a peek at the system internals.



This is where OS actually handles everything. So how does Instrumentation start the Activity exactly? Well, the param this in the execStartActivity method above is your Activity, i.e. the Context, and the execStartActivity makes use of this context.



A 30,000 overview is this: the Instrumentation class keeps tracks of a list of Activities that it’s monitoring in order to do it’s work. This list is used to coordinate all of the activities and make sure everything runs smoothly in managing the flow of activities.



There are some operations which I haven’t fully looked into which coordinate thread and process issues. Ultimately, the ActivityResult uses a native operation - ActivityManagerNative.getDefault().startActivity() which uses the Context that you passed in when you called startActivity. The context you passed in is used to assist in “intent resolution” if needed. Intent resolution is the process by which the system can determine the target of the intent if it is not supplied. (Check out the guide here for more details).



And in order for Android to do this, it needs access to information that is supplied by Context. Specifically, the system needs to access to a ContentResolver so it can “determine the MIME type of the intent’s data".
This whole bit about how startActivity makes use of context was a bit complicated and I don’t fully understand the internals myself. My main point was just to illustrate how application-wide resources need to be accessed in order to perform many of the operations that are essential to an app. Context is what provides access to these resources.
A simpler example might be Views. We all know what you create a custom View by extending RelativeLayout or some other View class, you must provide a constructor that takes a Context as an argument. When you instantiate your custom View you pass in the context.
Why? Because the View needs to be able to have access to themes, resources, and other View configuration details.
View configuration is actually a great example. Each Context has various parameters (fields in Context’s implementations) that are set by the OS itself for things like the dimension or density of the display. It’s easy to see why this information is important for setting up Views, etc.



One final word:
For some reason people new to Android (and even people not so new) seem to completely forget about object-oriented programming when it comes to Android. For some reason, people try to bend their Android development to pre-conceived paradigms or learned behaviors.



Android has it’s own paradigm and a certain pattern that is actually quite consistent if let go of your pre-conceived notions and simply read the documentation and dev guide. My real point, however, while “getting the right context” can sometimes be tricky, people unjustifiably panic because they run into a situation where they need the context and think they don’t have it. Once again, Java is an object-oriented language with an inheritance design.



You only “have” the context inside of your Activity because your activity itself inherits from Context. There’s no magic to it (except for the all the stuff the OS does by itself to set various parameters and to correctly “configure” your context). So, putting memory/performance issues aside (e.g. holding references to context when you don’t need to or doing it in a way that has negative consequences on memory, etc), Context is an object like any other and it can be passed around just like any POJO (Plain Old Java Object).
Sometimes you might need to do clever things to retrieve that context, but any regular Java class that extends from nothing other than Object itself can be written in a way that has access to context; simply expose a public method that takes a context and then use it in that class as needed. This was not intended as an exhaustive treatment on Context or Android internals, but I hope it’s helpful in demystifying Context a little bit.






share|improve this answer





















  • 19





    This is an excellent answer. Much better than the accepted one, which only says what everybody knows intuitively.

    – Honza Kalfus
    Feb 10 '17 at 10:44








  • 3





    this is the long answer you are looking for! well explained

    – Nick Jian
    Mar 29 '17 at 16:25






  • 4





    Excellent ans ! Seriously !

    – ZeekHuge
    Jun 9 '17 at 7:46






  • 1





    Hmmm, to me it all sounds like what we old timers used to call global variables, which was much frowned on when object orientation entered the scene 8-)

    – Ulf Edholm
    Aug 7 '17 at 20:55








  • 1





    This should be the accepted answer.

    – AymenDaoudi
    Dec 14 '17 at 5:24



















106














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.



For more information, look in Introduction to Android development with Android Studio - Tutorial.






share|improve this answer

































    72














    Context is an "interface" to the global information about an application environment. In practice, Context is actually 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.



    In the following picture, you can see a hierarchy of classes, where Context is the root class of this hierarchy. In particular, it's worth emphasizing that Activity is a descendant of Context.



    Activity diagram






    share|improve this answer

































      62














      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.






      share|improve this answer

































        41














        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.






        share|improve this answer

































          29














          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, 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 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 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





          share|improve this answer

































            20
















            • Context represents a handle to get environment data .


            • Context class itself is declared as abstract, whose implementation is provided by the android OS.


            • Context is like remote of a TV & channel's in the television are resources, services, etc.
              enter image description here


            What can you do with it ?




            • Loading resource.

            • Launching a new activity.

            • Creating views.

            • Obtaining system service.


            Ways to get context :




            • getApplicationContext()

            • getContext()


            • getBaseContext()enter image description hereenter image description here






            share|improve this answer

































              16














              Just putting it out there for newbies;



              So First understand Word Context :



              In english-lib. it means:




              "The circumstances that form the setting for an event, statement, or
              idea, and in terms of which it can be fully understood and assessed."



              "The parts of something written or spoken that immediately precede and
              follow a word or passage and clarify its meaning."




              Now take the same understanding to programming world:



              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, package/application)



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



              To Get Context Anywhere in application use following code:



              Create new class AppContext inside your android application



              public class AppContext extends Application {

              private static Context context;

              public void onCreate(){
              super.onCreate();
              AppContext.context = getApplicationContext();
              }

              public static Context getAppContext() {
              return AppContext.context;
              }
              }


              Now any time you want application context in non-activity class, call this method and you have application context.



              Hope this help ;)






              share|improve this answer

































                15














                Think of it as the VM that has siloed the process the app or service is running in. The siloed environment has access to a bunch of underlying system information and certain permitted resources. You need that context to get at those services.






                share|improve this answer

































                  14














                  Context is a reference to the current object as this. Also context allows access to information about the application environment.






                  share|improve this answer

































                    11














                    The class android.content.Context provides the connection to the Android system and the resources of the project. It is the interface to global information about the application environment.



                    The Context also provides access to Android Services, e.g. the Location Service.



                    Activities and Services extend the Context class.






                    share|improve this answer

































                      7














                      Context is basically for resource access and getting the environment details of the application(for application context) or activity (for activity context) or any other...



                      In order to avoid memory leak you should use application context for every components that needs a context object.... for more click here






                      share|improve this answer































                        7














                        Context is context of current state of the application/object.Its an entity that represents various environment data . Context helps the current activity to interact with out side android environment like local files, databases, class loaders associated to the environment, services including system-level services, and more.



                        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. It’s like a handle to the environment your application is currently running in. The activity object inherits the Context object.



                        Different invoking methods by which you can get context
                        1. getApplicationContext(),
                        2. getContext(),
                        3. getBaseContext()
                        4. or this (when in the activity class).






                        share|improve this answer































                          7














                          Context is Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                          It also gives access to the resources of the project. It is the interface to global information about the application environment.



                          The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                          Activities and services extend the Context class. Therefore they can be directly used to access the Context.






                          share|improve this answer































                            7














                            Context is an interface to global information about an application environment. It's an abstract class whose implementation is provided by the Android system.



                            Context allows access to application-specific resources and classes, as well as calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.



                            Here is Example



                             public class MyActivity extends Activity {

                            public void Testing() {

                            Context actContext = this; /*returns the Activity Context since Activity extends Context.*/

                            Context appContext = getApplicationContext(); /*returns the context of the single, global Application object of the current process. */

                            Button BtnShowAct1 = (Button) findViewById(R.id.btnGoToAct1);
                            Context BtnContext = BtnShowAct1.getContext(); /*returns the context of the View. */


                            For more details you can visit http://developer.android.com/reference/android/content/Context.html






                            share|improve this answer































                              4














                              Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                              It also gives access to the resources of the project. It is the interface to global information about the application environment.



                              The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                              Activities and services extend the Context class. Therefore they can be directly used to access the Context.






                              share|improve this answer































                                4














                                A Context is what most of us would call Application. It's made by the Android system and is able to do only what an application is able to.
                                In Tomcat, a Context is also what I would call an application.



                                There is one Context that holds many Activities, each Activity may have many Views.



                                Obviously, some will say that it doesn't fit because of this or that and they are probably right, but saying that a Context is your current application will help you to understand what you are putting in method parameters.






                                share|improve this answer

































                                  4














                                  Boss Assistant Analogy



                                  Lets have a small analogy before diving deep in the technicality of Context




                                  Every Boss has an assistant or someone( errand boy) who does less
                                  important and more time-consuming things for him. For example, if they
                                  need a file or coffee then an assistant will be on run. Boss will not
                                  know what is going on in the background but the file or the task will
                                  be delivered



                                  So Here

                                  Boss - Android Application

                                  Assistant - Context

                                  File or cup of coffee - Resource




                                  What official Android Developer site says about Context



                                  Context is your access point for application related resources



                                  Let's see some of such resources or tasks




                                  • Launching an activity.


                                  • Getting absolute path to the application specific cache directory on
                                    the filesystem.


                                  • Determining whether the given permission is allowed for a particular
                                    process and user ID running in the system.


                                  • Checking whether you have been granted a particular permission.



                                  And so on.

                                  So if an Android application wants to start an activity, it goes straight to Context (Access Point), and the Context class gives him back the resources(Intent in this case).



                                  Like any other class Context class too has fields and methods.

                                  You can explore more about Context in official documentation, it covers pretty much everything, available methods, fields and even how to use fields with methods.






                                  share|improve this answer


























                                  • you've just copied an answer from below???

                                    – Dilshad Abduwali
                                    Sep 22 '17 at 6:11











                                  • thanks for heads up @dhssa . I was trying to provide the gist of Android docs .. Shoul've been more careful . Removed the "copied" part

                                    – Rohit Singh
                                    Sep 22 '17 at 6:56





















                                  3














                                  Context means Android get to know in which activity I should go for or act in.



                                  1 - Toast.makeText(context, "Enter All Details", Toast.LENGTH_SHORT).show();
                                  it used in this.
                                  Context context = ActivityName.this;



                                  2 -startActivity(new Intent(context,LoginActivity.class));



                                  in this context means from which activity you wanna go to other activity.
                                  context or ActivityName.this is faster then , getContext and getApplicatinContext.






                                  share|improve this answer































                                    1














                                    If you want to connect Context with other familiar classes in Android, keep in mind this structure:




                                    Context < ContextWrapper < Application



                                    Context < ContextWrapper < ContextThemeWrapper < Activity



                                    Context < ContextWrapper < ContextThemeWrapper < Activity <
                                    ListActivity



                                    Context < ContextWrapper < Service



                                    Context < ContextWrapper < Service < IntentService




                                    So, all of those classes are contexts in their own way. You can cast Service and ListActivity to Context if you wish. But if you look closely, some of the classes inherit theme as well. In activity or fragment, you would like theming to be applied to your views, but don't care about it Service class, for instance.



                                    I explain the difference in contexts here.






                                    share|improve this answer































                                      1














                                      Putting simple, Androids Context is a mess that you won't love until you stop worrying about.



                                      Android Contexts are:




                                      • God-objects.



                                      • Thing that you want to pass around all your application when you are starting developing for Android, but will avoid doing it when you get a little bit closer to programming, testing and Android itself.




                                        • Unclear dependency.


                                        • Common source of memory leaks.


                                        • PITA for testing.




                                      • Actual context used by Android system to dispatch permissions, resources, preferences, services, broadcasts, styles, showing dialogs and inflating layout. And you need different Context instances for some separate things (obviously, you can't show a dialog from an application or service context; layouts inflated from application and activity contexts may differ).







                                      share|improve this answer

































                                        1














                                        The Context is the android specific api to each app-s Sandbox
                                        that provides access app private data like to resources, database, private filedirectories, preferences, settings ...



                                        Most of the privatedata are the same for all activities/services/broadcastlisteners of one application.



                                        Since Application, Activity, Service implement the Context interface they can be used where an api call needs a Context parameter






                                        share|improve this answer

































                                          0














                                          Context means component (or application) in various time-period. If I do eat so many food between 1 to 2 pm then my context of that time is used to access all methods (or resources) that I use during that time. Content is an component (application) for particular time. Context of components of application keeps changing based on the underlying lifecycle of the components or application.
                                          For instance, inside the onCreate() of an Activity,



                                          getBaseContext() -- gives the context of the Activity that is set (created) by the constructor of activity.
                                          getApplicationContext() -- gives the Context setup (created) during the creation of application.



                                          Note: <application> holds all Android Components.



                                          <application>
                                          <activity> .. </activity>

                                          <service> .. </service>

                                          <receiver> .. </receiver>

                                          <provider> .. </provider>
                                          </application>


                                          It means, when you call getApplicationContext() from inside whatever component, you are calling the common context of the whole application.



                                          Context keeps being modified by the system based on the lifecycle of components.






                                          share|improve this answer

































                                            0














                                             Context means current.
                                            Context use to do operation for current screen.
                                            ex.

                                              1. getApplicationContext()

                                              2. getContext()



                                            Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();





                                            share|improve this answer

































                                              -6














                                              This attribute declares which activity this layout is associated with by default.






                                              share|improve this answer































                                                -6














                                                In Java, we say this keyword refers to the state of the current object of the application.


                                                Similarly, in an alternate we have Context in Android Development.



                                                This can be defined either explicitly or implicitly,



                                                Context con = this;

                                                getApplicationContext();

                                                getBaseContext();

                                                getContext();





                                                share|improve this answer


























                                                • Could be just the object of the current activity I think. I use it as if it were this way a few times.

                                                  – Ohiovr
                                                  Dec 27 '16 at 0:56






                                                • 1





                                                  There is a large variety of ways to get context. Calling this only works in activities. Context isn't an alternative to "this" - context is used to access system features, and a lot more. You are over-simplifying Context and missing the main point with it.

                                                  – Zoe
                                                  Jul 14 '17 at 15:18



















                                                28 Answers
                                                28






                                                active

                                                oldest

                                                votes








                                                28 Answers
                                                28






                                                active

                                                oldest

                                                votes









                                                active

                                                oldest

                                                votes






                                                active

                                                oldest

                                                votes









                                                1321














                                                Putting it simply:



                                                As the name suggests, 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).



                                                You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in a class that extends from Context, such as the Application, Activity, Service and IntentService classes).



                                                Typical uses of context:





                                                • Creating new objects:
                                                  Creating new views, adapters, listeners:



                                                  TextView tv = new TextView(getContext());
                                                  ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);



                                                • Accessing standard common resources:
                                                  Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:



                                                  context.getSystemService(LAYOUT_INFLATER_SERVICE)
                                                  getApplicationContext().getSharedPreferences(*name*, *mode*);



                                                • Accessing components implicitly:
                                                  Regarding content providers, broadcasts, intent



                                                  getApplicationContext().getContentResolver().query(uri, ...);







                                                share|improve this answer





















                                                • 45





                                                  In your example context.getSystemService(LAYOUT_INFLATER_SERVICE), where and how is context defined?

                                                  – Dennis
                                                  Dec 4 '12 at 21:47






                                                • 4





                                                  It is well explained why we need context while dynamically creating a text view. But while dynamically creating arrays of text views we don't have to mention any context. Why is that so ? TextView textview = new TextView[10];

                                                  – Abhinav Arora
                                                  Dec 26 '14 at 8:09






                                                • 26





                                                  @AbhinavArora when defining the array, you're not actually constructing an instance of any text views (you're basically just creating the space for them to go into). At the point you come to put any values into that array, you'll either need pre-created TextViews, or need a Context to allow you to create them.

                                                  – mc1arke
                                                  Mar 18 '15 at 7:57






                                                • 17





                                                  Why do views need context? For example, what would TextView not be able to do if it didn't have the context?

                                                  – dinosaur
                                                  Apr 22 '16 at 17:33






                                                • 7





                                                  A piece of code without a "Context" can run on each operating systems that has JVM. But if there is a context it should run on Android.If you want to use Android specific things(accessing device location, taking a photo, running a background service etc.) you need a context Although you don't need if you make an http request. Context can be assumed as a bridge between Java and Android.

                                                  – Faruk Toptas
                                                  Jun 16 '16 at 12:14


















                                                1321














                                                Putting it simply:



                                                As the name suggests, 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).



                                                You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in a class that extends from Context, such as the Application, Activity, Service and IntentService classes).



                                                Typical uses of context:





                                                • Creating new objects:
                                                  Creating new views, adapters, listeners:



                                                  TextView tv = new TextView(getContext());
                                                  ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);



                                                • Accessing standard common resources:
                                                  Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:



                                                  context.getSystemService(LAYOUT_INFLATER_SERVICE)
                                                  getApplicationContext().getSharedPreferences(*name*, *mode*);



                                                • Accessing components implicitly:
                                                  Regarding content providers, broadcasts, intent



                                                  getApplicationContext().getContentResolver().query(uri, ...);







                                                share|improve this answer





















                                                • 45





                                                  In your example context.getSystemService(LAYOUT_INFLATER_SERVICE), where and how is context defined?

                                                  – Dennis
                                                  Dec 4 '12 at 21:47






                                                • 4





                                                  It is well explained why we need context while dynamically creating a text view. But while dynamically creating arrays of text views we don't have to mention any context. Why is that so ? TextView textview = new TextView[10];

                                                  – Abhinav Arora
                                                  Dec 26 '14 at 8:09






                                                • 26





                                                  @AbhinavArora when defining the array, you're not actually constructing an instance of any text views (you're basically just creating the space for them to go into). At the point you come to put any values into that array, you'll either need pre-created TextViews, or need a Context to allow you to create them.

                                                  – mc1arke
                                                  Mar 18 '15 at 7:57






                                                • 17





                                                  Why do views need context? For example, what would TextView not be able to do if it didn't have the context?

                                                  – dinosaur
                                                  Apr 22 '16 at 17:33






                                                • 7





                                                  A piece of code without a "Context" can run on each operating systems that has JVM. But if there is a context it should run on Android.If you want to use Android specific things(accessing device location, taking a photo, running a background service etc.) you need a context Although you don't need if you make an http request. Context can be assumed as a bridge between Java and Android.

                                                  – Faruk Toptas
                                                  Jun 16 '16 at 12:14
















                                                1321












                                                1321








                                                1321







                                                Putting it simply:



                                                As the name suggests, 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).



                                                You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in a class that extends from Context, such as the Application, Activity, Service and IntentService classes).



                                                Typical uses of context:





                                                • Creating new objects:
                                                  Creating new views, adapters, listeners:



                                                  TextView tv = new TextView(getContext());
                                                  ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);



                                                • Accessing standard common resources:
                                                  Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:



                                                  context.getSystemService(LAYOUT_INFLATER_SERVICE)
                                                  getApplicationContext().getSharedPreferences(*name*, *mode*);



                                                • Accessing components implicitly:
                                                  Regarding content providers, broadcasts, intent



                                                  getApplicationContext().getContentResolver().query(uri, ...);







                                                share|improve this answer















                                                Putting it simply:



                                                As the name suggests, 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).



                                                You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in a class that extends from Context, such as the Application, Activity, Service and IntentService classes).



                                                Typical uses of context:





                                                • Creating new objects:
                                                  Creating new views, adapters, listeners:



                                                  TextView tv = new TextView(getContext());
                                                  ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);



                                                • Accessing standard common resources:
                                                  Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:



                                                  context.getSystemService(LAYOUT_INFLATER_SERVICE)
                                                  getApplicationContext().getSharedPreferences(*name*, *mode*);



                                                • Accessing components implicitly:
                                                  Regarding content providers, broadcasts, intent



                                                  getApplicationContext().getContentResolver().query(uri, ...);








                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jan 4 '17 at 17:15









                                                Tim Castelijns

                                                31k1288110




                                                31k1288110










                                                answered Aug 26 '10 at 6:32









                                                Sameer SegalSameer Segal

                                                16.5k63453




                                                16.5k63453








                                                • 45





                                                  In your example context.getSystemService(LAYOUT_INFLATER_SERVICE), where and how is context defined?

                                                  – Dennis
                                                  Dec 4 '12 at 21:47






                                                • 4





                                                  It is well explained why we need context while dynamically creating a text view. But while dynamically creating arrays of text views we don't have to mention any context. Why is that so ? TextView textview = new TextView[10];

                                                  – Abhinav Arora
                                                  Dec 26 '14 at 8:09






                                                • 26





                                                  @AbhinavArora when defining the array, you're not actually constructing an instance of any text views (you're basically just creating the space for them to go into). At the point you come to put any values into that array, you'll either need pre-created TextViews, or need a Context to allow you to create them.

                                                  – mc1arke
                                                  Mar 18 '15 at 7:57






                                                • 17





                                                  Why do views need context? For example, what would TextView not be able to do if it didn't have the context?

                                                  – dinosaur
                                                  Apr 22 '16 at 17:33






                                                • 7





                                                  A piece of code without a "Context" can run on each operating systems that has JVM. But if there is a context it should run on Android.If you want to use Android specific things(accessing device location, taking a photo, running a background service etc.) you need a context Although you don't need if you make an http request. Context can be assumed as a bridge between Java and Android.

                                                  – Faruk Toptas
                                                  Jun 16 '16 at 12:14
















                                                • 45





                                                  In your example context.getSystemService(LAYOUT_INFLATER_SERVICE), where and how is context defined?

                                                  – Dennis
                                                  Dec 4 '12 at 21:47






                                                • 4





                                                  It is well explained why we need context while dynamically creating a text view. But while dynamically creating arrays of text views we don't have to mention any context. Why is that so ? TextView textview = new TextView[10];

                                                  – Abhinav Arora
                                                  Dec 26 '14 at 8:09






                                                • 26





                                                  @AbhinavArora when defining the array, you're not actually constructing an instance of any text views (you're basically just creating the space for them to go into). At the point you come to put any values into that array, you'll either need pre-created TextViews, or need a Context to allow you to create them.

                                                  – mc1arke
                                                  Mar 18 '15 at 7:57






                                                • 17





                                                  Why do views need context? For example, what would TextView not be able to do if it didn't have the context?

                                                  – dinosaur
                                                  Apr 22 '16 at 17:33






                                                • 7





                                                  A piece of code without a "Context" can run on each operating systems that has JVM. But if there is a context it should run on Android.If you want to use Android specific things(accessing device location, taking a photo, running a background service etc.) you need a context Although you don't need if you make an http request. Context can be assumed as a bridge between Java and Android.

                                                  – Faruk Toptas
                                                  Jun 16 '16 at 12:14










                                                45




                                                45





                                                In your example context.getSystemService(LAYOUT_INFLATER_SERVICE), where and how is context defined?

                                                – Dennis
                                                Dec 4 '12 at 21:47





                                                In your example context.getSystemService(LAYOUT_INFLATER_SERVICE), where and how is context defined?

                                                – Dennis
                                                Dec 4 '12 at 21:47




                                                4




                                                4





                                                It is well explained why we need context while dynamically creating a text view. But while dynamically creating arrays of text views we don't have to mention any context. Why is that so ? TextView textview = new TextView[10];

                                                – Abhinav Arora
                                                Dec 26 '14 at 8:09





                                                It is well explained why we need context while dynamically creating a text view. But while dynamically creating arrays of text views we don't have to mention any context. Why is that so ? TextView textview = new TextView[10];

                                                – Abhinav Arora
                                                Dec 26 '14 at 8:09




                                                26




                                                26





                                                @AbhinavArora when defining the array, you're not actually constructing an instance of any text views (you're basically just creating the space for them to go into). At the point you come to put any values into that array, you'll either need pre-created TextViews, or need a Context to allow you to create them.

                                                – mc1arke
                                                Mar 18 '15 at 7:57





                                                @AbhinavArora when defining the array, you're not actually constructing an instance of any text views (you're basically just creating the space for them to go into). At the point you come to put any values into that array, you'll either need pre-created TextViews, or need a Context to allow you to create them.

                                                – mc1arke
                                                Mar 18 '15 at 7:57




                                                17




                                                17





                                                Why do views need context? For example, what would TextView not be able to do if it didn't have the context?

                                                – dinosaur
                                                Apr 22 '16 at 17:33





                                                Why do views need context? For example, what would TextView not be able to do if it didn't have the context?

                                                – dinosaur
                                                Apr 22 '16 at 17:33




                                                7




                                                7





                                                A piece of code without a "Context" can run on each operating systems that has JVM. But if there is a context it should run on Android.If you want to use Android specific things(accessing device location, taking a photo, running a background service etc.) you need a context Although you don't need if you make an http request. Context can be assumed as a bridge between Java and Android.

                                                – Faruk Toptas
                                                Jun 16 '16 at 12:14







                                                A piece of code without a "Context" can run on each operating systems that has JVM. But if there is a context it should run on Android.If you want to use Android specific things(accessing device location, taking a photo, running a background service etc.) you need a context Although you don't need if you make an http request. Context can be assumed as a bridge between Java and Android.

                                                – Faruk Toptas
                                                Jun 16 '16 at 12:14















                                                444














                                                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 Activity, Service, Application, 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.






                                                share|improve this answer





















                                                • 3





                                                  Ok, so the class derived from the Activity IS a context itself. That is why by passing this to the newly created views, we pass the context.

                                                  – Oleg
                                                  Jan 25 '15 at 14:40






                                                • 4





                                                  I wonder if it's a good design decision to have the context accessible from so many different places? One static getContext() in the application would have been enough in my opinion.

                                                  – Trilarion
                                                  Dec 28 '15 at 9:05











                                                • @Trilarion... It depends on how you want to use context getApplicationContext(), getContext(),getBaseContext() ..... Refer this - > (stackoverflow.com/a/10641257)

                                                  – Devrath
                                                  Aug 14 '16 at 15:00











                                                • Just to expand the last piece with creating a textview: In some cases it may be necessary to call SomeActivityName.this. IN a thread for an instance, this refers to teh thread and not the activity

                                                  – Zoe
                                                  May 14 '17 at 10:37











                                                • Is context object unique for an apk or Android OS? Can an application have two different contexts?

                                                  – valijon
                                                  Aug 30 '18 at 12:14
















                                                444














                                                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 Activity, Service, Application, 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.






                                                share|improve this answer





















                                                • 3





                                                  Ok, so the class derived from the Activity IS a context itself. That is why by passing this to the newly created views, we pass the context.

                                                  – Oleg
                                                  Jan 25 '15 at 14:40






                                                • 4





                                                  I wonder if it's a good design decision to have the context accessible from so many different places? One static getContext() in the application would have been enough in my opinion.

                                                  – Trilarion
                                                  Dec 28 '15 at 9:05











                                                • @Trilarion... It depends on how you want to use context getApplicationContext(), getContext(),getBaseContext() ..... Refer this - > (stackoverflow.com/a/10641257)

                                                  – Devrath
                                                  Aug 14 '16 at 15:00











                                                • Just to expand the last piece with creating a textview: In some cases it may be necessary to call SomeActivityName.this. IN a thread for an instance, this refers to teh thread and not the activity

                                                  – Zoe
                                                  May 14 '17 at 10:37











                                                • Is context object unique for an apk or Android OS? Can an application have two different contexts?

                                                  – valijon
                                                  Aug 30 '18 at 12:14














                                                444












                                                444








                                                444







                                                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 Activity, Service, Application, 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.






                                                share|improve this answer















                                                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 Activity, Service, Application, 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.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Oct 26 '18 at 12:47









                                                nbro

                                                5,60084994




                                                5,60084994










                                                answered Aug 20 '13 at 5:25









                                                DevrathDevrath

                                                22.4k38132186




                                                22.4k38132186








                                                • 3





                                                  Ok, so the class derived from the Activity IS a context itself. That is why by passing this to the newly created views, we pass the context.

                                                  – Oleg
                                                  Jan 25 '15 at 14:40






                                                • 4





                                                  I wonder if it's a good design decision to have the context accessible from so many different places? One static getContext() in the application would have been enough in my opinion.

                                                  – Trilarion
                                                  Dec 28 '15 at 9:05











                                                • @Trilarion... It depends on how you want to use context getApplicationContext(), getContext(),getBaseContext() ..... Refer this - > (stackoverflow.com/a/10641257)

                                                  – Devrath
                                                  Aug 14 '16 at 15:00











                                                • Just to expand the last piece with creating a textview: In some cases it may be necessary to call SomeActivityName.this. IN a thread for an instance, this refers to teh thread and not the activity

                                                  – Zoe
                                                  May 14 '17 at 10:37











                                                • Is context object unique for an apk or Android OS? Can an application have two different contexts?

                                                  – valijon
                                                  Aug 30 '18 at 12:14














                                                • 3





                                                  Ok, so the class derived from the Activity IS a context itself. That is why by passing this to the newly created views, we pass the context.

                                                  – Oleg
                                                  Jan 25 '15 at 14:40






                                                • 4





                                                  I wonder if it's a good design decision to have the context accessible from so many different places? One static getContext() in the application would have been enough in my opinion.

                                                  – Trilarion
                                                  Dec 28 '15 at 9:05











                                                • @Trilarion... It depends on how you want to use context getApplicationContext(), getContext(),getBaseContext() ..... Refer this - > (stackoverflow.com/a/10641257)

                                                  – Devrath
                                                  Aug 14 '16 at 15:00











                                                • Just to expand the last piece with creating a textview: In some cases it may be necessary to call SomeActivityName.this. IN a thread for an instance, this refers to teh thread and not the activity

                                                  – Zoe
                                                  May 14 '17 at 10:37











                                                • Is context object unique for an apk or Android OS? Can an application have two different contexts?

                                                  – valijon
                                                  Aug 30 '18 at 12:14








                                                3




                                                3





                                                Ok, so the class derived from the Activity IS a context itself. That is why by passing this to the newly created views, we pass the context.

                                                – Oleg
                                                Jan 25 '15 at 14:40





                                                Ok, so the class derived from the Activity IS a context itself. That is why by passing this to the newly created views, we pass the context.

                                                – Oleg
                                                Jan 25 '15 at 14:40




                                                4




                                                4





                                                I wonder if it's a good design decision to have the context accessible from so many different places? One static getContext() in the application would have been enough in my opinion.

                                                – Trilarion
                                                Dec 28 '15 at 9:05





                                                I wonder if it's a good design decision to have the context accessible from so many different places? One static getContext() in the application would have been enough in my opinion.

                                                – Trilarion
                                                Dec 28 '15 at 9:05













                                                @Trilarion... It depends on how you want to use context getApplicationContext(), getContext(),getBaseContext() ..... Refer this - > (stackoverflow.com/a/10641257)

                                                – Devrath
                                                Aug 14 '16 at 15:00





                                                @Trilarion... It depends on how you want to use context getApplicationContext(), getContext(),getBaseContext() ..... Refer this - > (stackoverflow.com/a/10641257)

                                                – Devrath
                                                Aug 14 '16 at 15:00













                                                Just to expand the last piece with creating a textview: In some cases it may be necessary to call SomeActivityName.this. IN a thread for an instance, this refers to teh thread and not the activity

                                                – Zoe
                                                May 14 '17 at 10:37





                                                Just to expand the last piece with creating a textview: In some cases it may be necessary to call SomeActivityName.this. IN a thread for an instance, this refers to teh thread and not the activity

                                                – Zoe
                                                May 14 '17 at 10:37













                                                Is context object unique for an apk or Android OS? Can an application have two different contexts?

                                                – valijon
                                                Aug 30 '18 at 12:14





                                                Is context object unique for an apk or Android OS? Can an application have two different contexts?

                                                – valijon
                                                Aug 30 '18 at 12:14











                                                281














                                                Source





                                                The topic of Context in Android seems to be confusing to many. People just know that Context is needed quite often to do basic things in Android. People sometimes panic because they try to do perform some operation that requires the Context and they don’t know how to “get” the right Context. I’m going to try to demystify the idea of Context in Android. A full treatment of the issue is beyond the scope of this post, but I’ll try to give a general overview so that you have a sense of what Context is and how to use it. To understand what Context is, let’s take a look at the source code:



                                                https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/Context.java



                                                What exactly is Context?



                                                Well, the documentation itself provides a rather straightforward explanation: The Context class is an “Interface to global information about an application environment".



                                                The Context class itself is declared as abstract class, whose implementation is provided by the Android OS. The documentation further provides that Context “…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 understand very well, now, why the name is Context. It’s because it’s just that. The Context provides the link or hook, if you will, for an Activity, Service, or any other component, thereby linking it to the system, enabling access to the global application environment.
                                                In other words: the Context provides the answer to the components question of “where the hell am I in relation to app generally and how do I access/communicate with the rest of the app?” If this all seems a bit confusing, a quick look at the methods exposed by the Context class provides some further clues about its true nature.



                                                Here’s a random sampling of those methods:





                                                1. getAssets()

                                                2. getResources()

                                                3. getPackageManager()

                                                4. getString()

                                                5. getSharedPrefsFile()


                                                What do all these methods have in common? They all enable whoever has access to the Context to be able to access application-wide resources.



                                                Context, in other words, hooks the component that has a reference to it to the rest of application environment. The assets (think ’/assets’ folder in your project), for example, are available across the application, provided that an Activity, Service or whatever knows how to access those resources.
                                                Same goes for getResources() which allows to do things like getResources().getColor() which will hook you into the colors.xml resource (nevermind that aapt enables access to resources via java code, that’s a separate issue).



                                                The upshot is that Context is what enables access to system resources and its what hooks components into the “greater app".
                                                Let’s look at the subclasses of Context, the classes that provide the implementation of the abstract Context class.
                                                The most obvious class is the Activity class. Activity inherits from ContextThemeWrapper, which inherits from ContextWrapper, which inherits from Context itself.
                                                Those classes are useful to look at to understand things at a deeper level, but for now it’s sufficient to know that ContextThemeWrapper and ContextWrapper are pretty much what they sound like.
                                                They implement the abstract elements of the Context class itself by “wrapping” a context (the actual context) and delegating those functions to that context.
                                                An example is helpful - in the ContextWrapper class, the abstract method getAssets from the Context class is implemented as follows:



                                                @Override
                                                public AssetManager getAssets() {
                                                return mBase.getAssets();
                                                }


                                                mBase is simply a field set by the constructor to a specific context.
                                                So a context is wrapped and the ContextWrapper delegates its implementation of the getAssets method to that context. Let’s get back to examining the Activity class which ultimately inherits from Context to see how this all works.



                                                You probably know what an Activity is, but to review - it’s basically 'a single thing the user can do. It takes care of providing a window in which to place the UI that the user interacts with'.
                                                Developers familiar with other APIs and even non-developers might think of it vernacularly as a “screen.” That’s technically inaccurate, but it doesn’t matter for our purposes. So how do Activity and Context interact and what exactly is going in their inheritance relationship?



                                                Again, it’s helpful to look at specific examples. We all know how to launch Activities. Provided you have “the context” from which you are you are starting the Activity, you simply call startActivity(intent), where the Intent describes the context from which you are starting an Activity and the Activity you’d like to start. This is the familiar startActivity(this, SomeOtherActivity.class).



                                                And what is this? this is your Activity because the Activity class inherits from Context. The full scoop is like this: When you call startActivity, ultimately the Activity class executes something like this:



                                                Instrumentation.ActivityResult ar =
                                                mInstrumentation.execStartActivity(
                                                this, mMainThread.getApplicationThread(), mToken, this,
                                                intent, requestCode);


                                                So it utilizes the execStartActivity from the Instrumentation class (actually from an inner class in Instrumentation called ActivityResult).



                                                At this point, we are beginning to get a peek at the system internals.



                                                This is where OS actually handles everything. So how does Instrumentation start the Activity exactly? Well, the param this in the execStartActivity method above is your Activity, i.e. the Context, and the execStartActivity makes use of this context.



                                                A 30,000 overview is this: the Instrumentation class keeps tracks of a list of Activities that it’s monitoring in order to do it’s work. This list is used to coordinate all of the activities and make sure everything runs smoothly in managing the flow of activities.



                                                There are some operations which I haven’t fully looked into which coordinate thread and process issues. Ultimately, the ActivityResult uses a native operation - ActivityManagerNative.getDefault().startActivity() which uses the Context that you passed in when you called startActivity. The context you passed in is used to assist in “intent resolution” if needed. Intent resolution is the process by which the system can determine the target of the intent if it is not supplied. (Check out the guide here for more details).



                                                And in order for Android to do this, it needs access to information that is supplied by Context. Specifically, the system needs to access to a ContentResolver so it can “determine the MIME type of the intent’s data".
                                                This whole bit about how startActivity makes use of context was a bit complicated and I don’t fully understand the internals myself. My main point was just to illustrate how application-wide resources need to be accessed in order to perform many of the operations that are essential to an app. Context is what provides access to these resources.
                                                A simpler example might be Views. We all know what you create a custom View by extending RelativeLayout or some other View class, you must provide a constructor that takes a Context as an argument. When you instantiate your custom View you pass in the context.
                                                Why? Because the View needs to be able to have access to themes, resources, and other View configuration details.
                                                View configuration is actually a great example. Each Context has various parameters (fields in Context’s implementations) that are set by the OS itself for things like the dimension or density of the display. It’s easy to see why this information is important for setting up Views, etc.



                                                One final word:
                                                For some reason people new to Android (and even people not so new) seem to completely forget about object-oriented programming when it comes to Android. For some reason, people try to bend their Android development to pre-conceived paradigms or learned behaviors.



                                                Android has it’s own paradigm and a certain pattern that is actually quite consistent if let go of your pre-conceived notions and simply read the documentation and dev guide. My real point, however, while “getting the right context” can sometimes be tricky, people unjustifiably panic because they run into a situation where they need the context and think they don’t have it. Once again, Java is an object-oriented language with an inheritance design.



                                                You only “have” the context inside of your Activity because your activity itself inherits from Context. There’s no magic to it (except for the all the stuff the OS does by itself to set various parameters and to correctly “configure” your context). So, putting memory/performance issues aside (e.g. holding references to context when you don’t need to or doing it in a way that has negative consequences on memory, etc), Context is an object like any other and it can be passed around just like any POJO (Plain Old Java Object).
                                                Sometimes you might need to do clever things to retrieve that context, but any regular Java class that extends from nothing other than Object itself can be written in a way that has access to context; simply expose a public method that takes a context and then use it in that class as needed. This was not intended as an exhaustive treatment on Context or Android internals, but I hope it’s helpful in demystifying Context a little bit.






                                                share|improve this answer





















                                                • 19





                                                  This is an excellent answer. Much better than the accepted one, which only says what everybody knows intuitively.

                                                  – Honza Kalfus
                                                  Feb 10 '17 at 10:44








                                                • 3





                                                  this is the long answer you are looking for! well explained

                                                  – Nick Jian
                                                  Mar 29 '17 at 16:25






                                                • 4





                                                  Excellent ans ! Seriously !

                                                  – ZeekHuge
                                                  Jun 9 '17 at 7:46






                                                • 1





                                                  Hmmm, to me it all sounds like what we old timers used to call global variables, which was much frowned on when object orientation entered the scene 8-)

                                                  – Ulf Edholm
                                                  Aug 7 '17 at 20:55








                                                • 1





                                                  This should be the accepted answer.

                                                  – AymenDaoudi
                                                  Dec 14 '17 at 5:24
















                                                281














                                                Source





                                                The topic of Context in Android seems to be confusing to many. People just know that Context is needed quite often to do basic things in Android. People sometimes panic because they try to do perform some operation that requires the Context and they don’t know how to “get” the right Context. I’m going to try to demystify the idea of Context in Android. A full treatment of the issue is beyond the scope of this post, but I’ll try to give a general overview so that you have a sense of what Context is and how to use it. To understand what Context is, let’s take a look at the source code:



                                                https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/Context.java



                                                What exactly is Context?



                                                Well, the documentation itself provides a rather straightforward explanation: The Context class is an “Interface to global information about an application environment".



                                                The Context class itself is declared as abstract class, whose implementation is provided by the Android OS. The documentation further provides that Context “…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 understand very well, now, why the name is Context. It’s because it’s just that. The Context provides the link or hook, if you will, for an Activity, Service, or any other component, thereby linking it to the system, enabling access to the global application environment.
                                                In other words: the Context provides the answer to the components question of “where the hell am I in relation to app generally and how do I access/communicate with the rest of the app?” If this all seems a bit confusing, a quick look at the methods exposed by the Context class provides some further clues about its true nature.



                                                Here’s a random sampling of those methods:





                                                1. getAssets()

                                                2. getResources()

                                                3. getPackageManager()

                                                4. getString()

                                                5. getSharedPrefsFile()


                                                What do all these methods have in common? They all enable whoever has access to the Context to be able to access application-wide resources.



                                                Context, in other words, hooks the component that has a reference to it to the rest of application environment. The assets (think ’/assets’ folder in your project), for example, are available across the application, provided that an Activity, Service or whatever knows how to access those resources.
                                                Same goes for getResources() which allows to do things like getResources().getColor() which will hook you into the colors.xml resource (nevermind that aapt enables access to resources via java code, that’s a separate issue).



                                                The upshot is that Context is what enables access to system resources and its what hooks components into the “greater app".
                                                Let’s look at the subclasses of Context, the classes that provide the implementation of the abstract Context class.
                                                The most obvious class is the Activity class. Activity inherits from ContextThemeWrapper, which inherits from ContextWrapper, which inherits from Context itself.
                                                Those classes are useful to look at to understand things at a deeper level, but for now it’s sufficient to know that ContextThemeWrapper and ContextWrapper are pretty much what they sound like.
                                                They implement the abstract elements of the Context class itself by “wrapping” a context (the actual context) and delegating those functions to that context.
                                                An example is helpful - in the ContextWrapper class, the abstract method getAssets from the Context class is implemented as follows:



                                                @Override
                                                public AssetManager getAssets() {
                                                return mBase.getAssets();
                                                }


                                                mBase is simply a field set by the constructor to a specific context.
                                                So a context is wrapped and the ContextWrapper delegates its implementation of the getAssets method to that context. Let’s get back to examining the Activity class which ultimately inherits from Context to see how this all works.



                                                You probably know what an Activity is, but to review - it’s basically 'a single thing the user can do. It takes care of providing a window in which to place the UI that the user interacts with'.
                                                Developers familiar with other APIs and even non-developers might think of it vernacularly as a “screen.” That’s technically inaccurate, but it doesn’t matter for our purposes. So how do Activity and Context interact and what exactly is going in their inheritance relationship?



                                                Again, it’s helpful to look at specific examples. We all know how to launch Activities. Provided you have “the context” from which you are you are starting the Activity, you simply call startActivity(intent), where the Intent describes the context from which you are starting an Activity and the Activity you’d like to start. This is the familiar startActivity(this, SomeOtherActivity.class).



                                                And what is this? this is your Activity because the Activity class inherits from Context. The full scoop is like this: When you call startActivity, ultimately the Activity class executes something like this:



                                                Instrumentation.ActivityResult ar =
                                                mInstrumentation.execStartActivity(
                                                this, mMainThread.getApplicationThread(), mToken, this,
                                                intent, requestCode);


                                                So it utilizes the execStartActivity from the Instrumentation class (actually from an inner class in Instrumentation called ActivityResult).



                                                At this point, we are beginning to get a peek at the system internals.



                                                This is where OS actually handles everything. So how does Instrumentation start the Activity exactly? Well, the param this in the execStartActivity method above is your Activity, i.e. the Context, and the execStartActivity makes use of this context.



                                                A 30,000 overview is this: the Instrumentation class keeps tracks of a list of Activities that it’s monitoring in order to do it’s work. This list is used to coordinate all of the activities and make sure everything runs smoothly in managing the flow of activities.



                                                There are some operations which I haven’t fully looked into which coordinate thread and process issues. Ultimately, the ActivityResult uses a native operation - ActivityManagerNative.getDefault().startActivity() which uses the Context that you passed in when you called startActivity. The context you passed in is used to assist in “intent resolution” if needed. Intent resolution is the process by which the system can determine the target of the intent if it is not supplied. (Check out the guide here for more details).



                                                And in order for Android to do this, it needs access to information that is supplied by Context. Specifically, the system needs to access to a ContentResolver so it can “determine the MIME type of the intent’s data".
                                                This whole bit about how startActivity makes use of context was a bit complicated and I don’t fully understand the internals myself. My main point was just to illustrate how application-wide resources need to be accessed in order to perform many of the operations that are essential to an app. Context is what provides access to these resources.
                                                A simpler example might be Views. We all know what you create a custom View by extending RelativeLayout or some other View class, you must provide a constructor that takes a Context as an argument. When you instantiate your custom View you pass in the context.
                                                Why? Because the View needs to be able to have access to themes, resources, and other View configuration details.
                                                View configuration is actually a great example. Each Context has various parameters (fields in Context’s implementations) that are set by the OS itself for things like the dimension or density of the display. It’s easy to see why this information is important for setting up Views, etc.



                                                One final word:
                                                For some reason people new to Android (and even people not so new) seem to completely forget about object-oriented programming when it comes to Android. For some reason, people try to bend their Android development to pre-conceived paradigms or learned behaviors.



                                                Android has it’s own paradigm and a certain pattern that is actually quite consistent if let go of your pre-conceived notions and simply read the documentation and dev guide. My real point, however, while “getting the right context” can sometimes be tricky, people unjustifiably panic because they run into a situation where they need the context and think they don’t have it. Once again, Java is an object-oriented language with an inheritance design.



                                                You only “have” the context inside of your Activity because your activity itself inherits from Context. There’s no magic to it (except for the all the stuff the OS does by itself to set various parameters and to correctly “configure” your context). So, putting memory/performance issues aside (e.g. holding references to context when you don’t need to or doing it in a way that has negative consequences on memory, etc), Context is an object like any other and it can be passed around just like any POJO (Plain Old Java Object).
                                                Sometimes you might need to do clever things to retrieve that context, but any regular Java class that extends from nothing other than Object itself can be written in a way that has access to context; simply expose a public method that takes a context and then use it in that class as needed. This was not intended as an exhaustive treatment on Context or Android internals, but I hope it’s helpful in demystifying Context a little bit.






                                                share|improve this answer





















                                                • 19





                                                  This is an excellent answer. Much better than the accepted one, which only says what everybody knows intuitively.

                                                  – Honza Kalfus
                                                  Feb 10 '17 at 10:44








                                                • 3





                                                  this is the long answer you are looking for! well explained

                                                  – Nick Jian
                                                  Mar 29 '17 at 16:25






                                                • 4





                                                  Excellent ans ! Seriously !

                                                  – ZeekHuge
                                                  Jun 9 '17 at 7:46






                                                • 1





                                                  Hmmm, to me it all sounds like what we old timers used to call global variables, which was much frowned on when object orientation entered the scene 8-)

                                                  – Ulf Edholm
                                                  Aug 7 '17 at 20:55








                                                • 1





                                                  This should be the accepted answer.

                                                  – AymenDaoudi
                                                  Dec 14 '17 at 5:24














                                                281












                                                281








                                                281







                                                Source





                                                The topic of Context in Android seems to be confusing to many. People just know that Context is needed quite often to do basic things in Android. People sometimes panic because they try to do perform some operation that requires the Context and they don’t know how to “get” the right Context. I’m going to try to demystify the idea of Context in Android. A full treatment of the issue is beyond the scope of this post, but I’ll try to give a general overview so that you have a sense of what Context is and how to use it. To understand what Context is, let’s take a look at the source code:



                                                https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/Context.java



                                                What exactly is Context?



                                                Well, the documentation itself provides a rather straightforward explanation: The Context class is an “Interface to global information about an application environment".



                                                The Context class itself is declared as abstract class, whose implementation is provided by the Android OS. The documentation further provides that Context “…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 understand very well, now, why the name is Context. It’s because it’s just that. The Context provides the link or hook, if you will, for an Activity, Service, or any other component, thereby linking it to the system, enabling access to the global application environment.
                                                In other words: the Context provides the answer to the components question of “where the hell am I in relation to app generally and how do I access/communicate with the rest of the app?” If this all seems a bit confusing, a quick look at the methods exposed by the Context class provides some further clues about its true nature.



                                                Here’s a random sampling of those methods:





                                                1. getAssets()

                                                2. getResources()

                                                3. getPackageManager()

                                                4. getString()

                                                5. getSharedPrefsFile()


                                                What do all these methods have in common? They all enable whoever has access to the Context to be able to access application-wide resources.



                                                Context, in other words, hooks the component that has a reference to it to the rest of application environment. The assets (think ’/assets’ folder in your project), for example, are available across the application, provided that an Activity, Service or whatever knows how to access those resources.
                                                Same goes for getResources() which allows to do things like getResources().getColor() which will hook you into the colors.xml resource (nevermind that aapt enables access to resources via java code, that’s a separate issue).



                                                The upshot is that Context is what enables access to system resources and its what hooks components into the “greater app".
                                                Let’s look at the subclasses of Context, the classes that provide the implementation of the abstract Context class.
                                                The most obvious class is the Activity class. Activity inherits from ContextThemeWrapper, which inherits from ContextWrapper, which inherits from Context itself.
                                                Those classes are useful to look at to understand things at a deeper level, but for now it’s sufficient to know that ContextThemeWrapper and ContextWrapper are pretty much what they sound like.
                                                They implement the abstract elements of the Context class itself by “wrapping” a context (the actual context) and delegating those functions to that context.
                                                An example is helpful - in the ContextWrapper class, the abstract method getAssets from the Context class is implemented as follows:



                                                @Override
                                                public AssetManager getAssets() {
                                                return mBase.getAssets();
                                                }


                                                mBase is simply a field set by the constructor to a specific context.
                                                So a context is wrapped and the ContextWrapper delegates its implementation of the getAssets method to that context. Let’s get back to examining the Activity class which ultimately inherits from Context to see how this all works.



                                                You probably know what an Activity is, but to review - it’s basically 'a single thing the user can do. It takes care of providing a window in which to place the UI that the user interacts with'.
                                                Developers familiar with other APIs and even non-developers might think of it vernacularly as a “screen.” That’s technically inaccurate, but it doesn’t matter for our purposes. So how do Activity and Context interact and what exactly is going in their inheritance relationship?



                                                Again, it’s helpful to look at specific examples. We all know how to launch Activities. Provided you have “the context” from which you are you are starting the Activity, you simply call startActivity(intent), where the Intent describes the context from which you are starting an Activity and the Activity you’d like to start. This is the familiar startActivity(this, SomeOtherActivity.class).



                                                And what is this? this is your Activity because the Activity class inherits from Context. The full scoop is like this: When you call startActivity, ultimately the Activity class executes something like this:



                                                Instrumentation.ActivityResult ar =
                                                mInstrumentation.execStartActivity(
                                                this, mMainThread.getApplicationThread(), mToken, this,
                                                intent, requestCode);


                                                So it utilizes the execStartActivity from the Instrumentation class (actually from an inner class in Instrumentation called ActivityResult).



                                                At this point, we are beginning to get a peek at the system internals.



                                                This is where OS actually handles everything. So how does Instrumentation start the Activity exactly? Well, the param this in the execStartActivity method above is your Activity, i.e. the Context, and the execStartActivity makes use of this context.



                                                A 30,000 overview is this: the Instrumentation class keeps tracks of a list of Activities that it’s monitoring in order to do it’s work. This list is used to coordinate all of the activities and make sure everything runs smoothly in managing the flow of activities.



                                                There are some operations which I haven’t fully looked into which coordinate thread and process issues. Ultimately, the ActivityResult uses a native operation - ActivityManagerNative.getDefault().startActivity() which uses the Context that you passed in when you called startActivity. The context you passed in is used to assist in “intent resolution” if needed. Intent resolution is the process by which the system can determine the target of the intent if it is not supplied. (Check out the guide here for more details).



                                                And in order for Android to do this, it needs access to information that is supplied by Context. Specifically, the system needs to access to a ContentResolver so it can “determine the MIME type of the intent’s data".
                                                This whole bit about how startActivity makes use of context was a bit complicated and I don’t fully understand the internals myself. My main point was just to illustrate how application-wide resources need to be accessed in order to perform many of the operations that are essential to an app. Context is what provides access to these resources.
                                                A simpler example might be Views. We all know what you create a custom View by extending RelativeLayout or some other View class, you must provide a constructor that takes a Context as an argument. When you instantiate your custom View you pass in the context.
                                                Why? Because the View needs to be able to have access to themes, resources, and other View configuration details.
                                                View configuration is actually a great example. Each Context has various parameters (fields in Context’s implementations) that are set by the OS itself for things like the dimension or density of the display. It’s easy to see why this information is important for setting up Views, etc.



                                                One final word:
                                                For some reason people new to Android (and even people not so new) seem to completely forget about object-oriented programming when it comes to Android. For some reason, people try to bend their Android development to pre-conceived paradigms or learned behaviors.



                                                Android has it’s own paradigm and a certain pattern that is actually quite consistent if let go of your pre-conceived notions and simply read the documentation and dev guide. My real point, however, while “getting the right context” can sometimes be tricky, people unjustifiably panic because they run into a situation where they need the context and think they don’t have it. Once again, Java is an object-oriented language with an inheritance design.



                                                You only “have” the context inside of your Activity because your activity itself inherits from Context. There’s no magic to it (except for the all the stuff the OS does by itself to set various parameters and to correctly “configure” your context). So, putting memory/performance issues aside (e.g. holding references to context when you don’t need to or doing it in a way that has negative consequences on memory, etc), Context is an object like any other and it can be passed around just like any POJO (Plain Old Java Object).
                                                Sometimes you might need to do clever things to retrieve that context, but any regular Java class that extends from nothing other than Object itself can be written in a way that has access to context; simply expose a public method that takes a context and then use it in that class as needed. This was not intended as an exhaustive treatment on Context or Android internals, but I hope it’s helpful in demystifying Context a little bit.






                                                share|improve this answer















                                                Source





                                                The topic of Context in Android seems to be confusing to many. People just know that Context is needed quite often to do basic things in Android. People sometimes panic because they try to do perform some operation that requires the Context and they don’t know how to “get” the right Context. I’m going to try to demystify the idea of Context in Android. A full treatment of the issue is beyond the scope of this post, but I’ll try to give a general overview so that you have a sense of what Context is and how to use it. To understand what Context is, let’s take a look at the source code:



                                                https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/Context.java



                                                What exactly is Context?



                                                Well, the documentation itself provides a rather straightforward explanation: The Context class is an “Interface to global information about an application environment".



                                                The Context class itself is declared as abstract class, whose implementation is provided by the Android OS. The documentation further provides that Context “…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 understand very well, now, why the name is Context. It’s because it’s just that. The Context provides the link or hook, if you will, for an Activity, Service, or any other component, thereby linking it to the system, enabling access to the global application environment.
                                                In other words: the Context provides the answer to the components question of “where the hell am I in relation to app generally and how do I access/communicate with the rest of the app?” If this all seems a bit confusing, a quick look at the methods exposed by the Context class provides some further clues about its true nature.



                                                Here’s a random sampling of those methods:





                                                1. getAssets()

                                                2. getResources()

                                                3. getPackageManager()

                                                4. getString()

                                                5. getSharedPrefsFile()


                                                What do all these methods have in common? They all enable whoever has access to the Context to be able to access application-wide resources.



                                                Context, in other words, hooks the component that has a reference to it to the rest of application environment. The assets (think ’/assets’ folder in your project), for example, are available across the application, provided that an Activity, Service or whatever knows how to access those resources.
                                                Same goes for getResources() which allows to do things like getResources().getColor() which will hook you into the colors.xml resource (nevermind that aapt enables access to resources via java code, that’s a separate issue).



                                                The upshot is that Context is what enables access to system resources and its what hooks components into the “greater app".
                                                Let’s look at the subclasses of Context, the classes that provide the implementation of the abstract Context class.
                                                The most obvious class is the Activity class. Activity inherits from ContextThemeWrapper, which inherits from ContextWrapper, which inherits from Context itself.
                                                Those classes are useful to look at to understand things at a deeper level, but for now it’s sufficient to know that ContextThemeWrapper and ContextWrapper are pretty much what they sound like.
                                                They implement the abstract elements of the Context class itself by “wrapping” a context (the actual context) and delegating those functions to that context.
                                                An example is helpful - in the ContextWrapper class, the abstract method getAssets from the Context class is implemented as follows:



                                                @Override
                                                public AssetManager getAssets() {
                                                return mBase.getAssets();
                                                }


                                                mBase is simply a field set by the constructor to a specific context.
                                                So a context is wrapped and the ContextWrapper delegates its implementation of the getAssets method to that context. Let’s get back to examining the Activity class which ultimately inherits from Context to see how this all works.



                                                You probably know what an Activity is, but to review - it’s basically 'a single thing the user can do. It takes care of providing a window in which to place the UI that the user interacts with'.
                                                Developers familiar with other APIs and even non-developers might think of it vernacularly as a “screen.” That’s technically inaccurate, but it doesn’t matter for our purposes. So how do Activity and Context interact and what exactly is going in their inheritance relationship?



                                                Again, it’s helpful to look at specific examples. We all know how to launch Activities. Provided you have “the context” from which you are you are starting the Activity, you simply call startActivity(intent), where the Intent describes the context from which you are starting an Activity and the Activity you’d like to start. This is the familiar startActivity(this, SomeOtherActivity.class).



                                                And what is this? this is your Activity because the Activity class inherits from Context. The full scoop is like this: When you call startActivity, ultimately the Activity class executes something like this:



                                                Instrumentation.ActivityResult ar =
                                                mInstrumentation.execStartActivity(
                                                this, mMainThread.getApplicationThread(), mToken, this,
                                                intent, requestCode);


                                                So it utilizes the execStartActivity from the Instrumentation class (actually from an inner class in Instrumentation called ActivityResult).



                                                At this point, we are beginning to get a peek at the system internals.



                                                This is where OS actually handles everything. So how does Instrumentation start the Activity exactly? Well, the param this in the execStartActivity method above is your Activity, i.e. the Context, and the execStartActivity makes use of this context.



                                                A 30,000 overview is this: the Instrumentation class keeps tracks of a list of Activities that it’s monitoring in order to do it’s work. This list is used to coordinate all of the activities and make sure everything runs smoothly in managing the flow of activities.



                                                There are some operations which I haven’t fully looked into which coordinate thread and process issues. Ultimately, the ActivityResult uses a native operation - ActivityManagerNative.getDefault().startActivity() which uses the Context that you passed in when you called startActivity. The context you passed in is used to assist in “intent resolution” if needed. Intent resolution is the process by which the system can determine the target of the intent if it is not supplied. (Check out the guide here for more details).



                                                And in order for Android to do this, it needs access to information that is supplied by Context. Specifically, the system needs to access to a ContentResolver so it can “determine the MIME type of the intent’s data".
                                                This whole bit about how startActivity makes use of context was a bit complicated and I don’t fully understand the internals myself. My main point was just to illustrate how application-wide resources need to be accessed in order to perform many of the operations that are essential to an app. Context is what provides access to these resources.
                                                A simpler example might be Views. We all know what you create a custom View by extending RelativeLayout or some other View class, you must provide a constructor that takes a Context as an argument. When you instantiate your custom View you pass in the context.
                                                Why? Because the View needs to be able to have access to themes, resources, and other View configuration details.
                                                View configuration is actually a great example. Each Context has various parameters (fields in Context’s implementations) that are set by the OS itself for things like the dimension or density of the display. It’s easy to see why this information is important for setting up Views, etc.



                                                One final word:
                                                For some reason people new to Android (and even people not so new) seem to completely forget about object-oriented programming when it comes to Android. For some reason, people try to bend their Android development to pre-conceived paradigms or learned behaviors.



                                                Android has it’s own paradigm and a certain pattern that is actually quite consistent if let go of your pre-conceived notions and simply read the documentation and dev guide. My real point, however, while “getting the right context” can sometimes be tricky, people unjustifiably panic because they run into a situation where they need the context and think they don’t have it. Once again, Java is an object-oriented language with an inheritance design.



                                                You only “have” the context inside of your Activity because your activity itself inherits from Context. There’s no magic to it (except for the all the stuff the OS does by itself to set various parameters and to correctly “configure” your context). So, putting memory/performance issues aside (e.g. holding references to context when you don’t need to or doing it in a way that has negative consequences on memory, etc), Context is an object like any other and it can be passed around just like any POJO (Plain Old Java Object).
                                                Sometimes you might need to do clever things to retrieve that context, but any regular Java class that extends from nothing other than Object itself can be written in a way that has access to context; simply expose a public method that takes a context and then use it in that class as needed. This was not intended as an exhaustive treatment on Context or Android internals, but I hope it’s helpful in demystifying Context a little bit.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Mar 29 '18 at 16:32









                                                Cindy Meister

                                                14.6k102234




                                                14.6k102234










                                                answered Apr 30 '13 at 13:44









                                                Sourab SharmaSourab Sharma

                                                5,30422029




                                                5,30422029








                                                • 19





                                                  This is an excellent answer. Much better than the accepted one, which only says what everybody knows intuitively.

                                                  – Honza Kalfus
                                                  Feb 10 '17 at 10:44








                                                • 3





                                                  this is the long answer you are looking for! well explained

                                                  – Nick Jian
                                                  Mar 29 '17 at 16:25






                                                • 4





                                                  Excellent ans ! Seriously !

                                                  – ZeekHuge
                                                  Jun 9 '17 at 7:46






                                                • 1





                                                  Hmmm, to me it all sounds like what we old timers used to call global variables, which was much frowned on when object orientation entered the scene 8-)

                                                  – Ulf Edholm
                                                  Aug 7 '17 at 20:55








                                                • 1





                                                  This should be the accepted answer.

                                                  – AymenDaoudi
                                                  Dec 14 '17 at 5:24














                                                • 19





                                                  This is an excellent answer. Much better than the accepted one, which only says what everybody knows intuitively.

                                                  – Honza Kalfus
                                                  Feb 10 '17 at 10:44








                                                • 3





                                                  this is the long answer you are looking for! well explained

                                                  – Nick Jian
                                                  Mar 29 '17 at 16:25






                                                • 4





                                                  Excellent ans ! Seriously !

                                                  – ZeekHuge
                                                  Jun 9 '17 at 7:46






                                                • 1





                                                  Hmmm, to me it all sounds like what we old timers used to call global variables, which was much frowned on when object orientation entered the scene 8-)

                                                  – Ulf Edholm
                                                  Aug 7 '17 at 20:55








                                                • 1





                                                  This should be the accepted answer.

                                                  – AymenDaoudi
                                                  Dec 14 '17 at 5:24








                                                19




                                                19





                                                This is an excellent answer. Much better than the accepted one, which only says what everybody knows intuitively.

                                                – Honza Kalfus
                                                Feb 10 '17 at 10:44







                                                This is an excellent answer. Much better than the accepted one, which only says what everybody knows intuitively.

                                                – Honza Kalfus
                                                Feb 10 '17 at 10:44






                                                3




                                                3





                                                this is the long answer you are looking for! well explained

                                                – Nick Jian
                                                Mar 29 '17 at 16:25





                                                this is the long answer you are looking for! well explained

                                                – Nick Jian
                                                Mar 29 '17 at 16:25




                                                4




                                                4





                                                Excellent ans ! Seriously !

                                                – ZeekHuge
                                                Jun 9 '17 at 7:46





                                                Excellent ans ! Seriously !

                                                – ZeekHuge
                                                Jun 9 '17 at 7:46




                                                1




                                                1





                                                Hmmm, to me it all sounds like what we old timers used to call global variables, which was much frowned on when object orientation entered the scene 8-)

                                                – Ulf Edholm
                                                Aug 7 '17 at 20:55







                                                Hmmm, to me it all sounds like what we old timers used to call global variables, which was much frowned on when object orientation entered the scene 8-)

                                                – Ulf Edholm
                                                Aug 7 '17 at 20:55






                                                1




                                                1





                                                This should be the accepted answer.

                                                – AymenDaoudi
                                                Dec 14 '17 at 5:24





                                                This should be the accepted answer.

                                                – AymenDaoudi
                                                Dec 14 '17 at 5:24











                                                106














                                                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.



                                                For more information, look in Introduction to Android development with Android Studio - Tutorial.






                                                share|improve this answer






























                                                  106














                                                  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.



                                                  For more information, look in Introduction to Android development with Android Studio - Tutorial.






                                                  share|improve this answer




























                                                    106












                                                    106








                                                    106







                                                    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.



                                                    For more information, look in Introduction to Android development with Android Studio - Tutorial.






                                                    share|improve this answer















                                                    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.



                                                    For more information, look in Introduction to Android development with Android Studio - Tutorial.







                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Oct 29 '18 at 1:27

























                                                    answered Aug 26 '10 at 6:33









                                                    angryITguyangryITguy

                                                    5,33384570




                                                    5,33384570























                                                        72














                                                        Context is an "interface" to the global information about an application environment. In practice, Context is actually 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.



                                                        In the following picture, you can see a hierarchy of classes, where Context is the root class of this hierarchy. In particular, it's worth emphasizing that Activity is a descendant of Context.



                                                        Activity diagram






                                                        share|improve this answer






























                                                          72














                                                          Context is an "interface" to the global information about an application environment. In practice, Context is actually 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.



                                                          In the following picture, you can see a hierarchy of classes, where Context is the root class of this hierarchy. In particular, it's worth emphasizing that Activity is a descendant of Context.



                                                          Activity diagram






                                                          share|improve this answer




























                                                            72












                                                            72








                                                            72







                                                            Context is an "interface" to the global information about an application environment. In practice, Context is actually 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.



                                                            In the following picture, you can see a hierarchy of classes, where Context is the root class of this hierarchy. In particular, it's worth emphasizing that Activity is a descendant of Context.



                                                            Activity diagram






                                                            share|improve this answer















                                                            Context is an "interface" to the global information about an application environment. In practice, Context is actually 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.



                                                            In the following picture, you can see a hierarchy of classes, where Context is the root class of this hierarchy. In particular, it's worth emphasizing that Activity is a descendant of Context.



                                                            Activity diagram







                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Oct 26 '18 at 16:10









                                                            nbro

                                                            5,60084994




                                                            5,60084994










                                                            answered Dec 28 '12 at 8:05









                                                            Dmytro DanylykDmytro Danylyk

                                                            16.3k85467




                                                            16.3k85467























                                                                62














                                                                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.






                                                                share|improve this answer






























                                                                  62














                                                                  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.






                                                                  share|improve this answer




























                                                                    62












                                                                    62








                                                                    62







                                                                    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.






                                                                    share|improve this answer















                                                                    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.







                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Oct 26 '18 at 15:51









                                                                    nbro

                                                                    5,60084994




                                                                    5,60084994










                                                                    answered Apr 28 '12 at 10:14









                                                                    s0ld13rs0ld13r

                                                                    62569




                                                                    62569























                                                                        41














                                                                        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.






                                                                        share|improve this answer






























                                                                          41














                                                                          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.






                                                                          share|improve this answer




























                                                                            41












                                                                            41








                                                                            41







                                                                            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.






                                                                            share|improve this answer















                                                                            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.







                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited Oct 26 '18 at 12:47









                                                                            nbro

                                                                            5,60084994




                                                                            5,60084994










                                                                            answered Aug 26 '10 at 6:40









                                                                            naikusnaikus

                                                                            21.3k43443




                                                                            21.3k43443























                                                                                29














                                                                                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, 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 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 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





                                                                                share|improve this answer






























                                                                                  29














                                                                                  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, 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 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 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





                                                                                  share|improve this answer




























                                                                                    29












                                                                                    29








                                                                                    29







                                                                                    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, 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 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 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





                                                                                    share|improve this answer















                                                                                    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, 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 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 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






                                                                                    share|improve this answer














                                                                                    share|improve this answer



                                                                                    share|improve this answer








                                                                                    edited May 2 '16 at 12:44

























                                                                                    answered May 2 '16 at 12:37









                                                                                    Parsania HardikParsania Hardik

                                                                                    3,18112431




                                                                                    3,18112431























                                                                                        20
















                                                                                        • Context represents a handle to get environment data .


                                                                                        • Context class itself is declared as abstract, whose implementation is provided by the android OS.


                                                                                        • Context is like remote of a TV & channel's in the television are resources, services, etc.
                                                                                          enter image description here


                                                                                        What can you do with it ?




                                                                                        • Loading resource.

                                                                                        • Launching a new activity.

                                                                                        • Creating views.

                                                                                        • Obtaining system service.


                                                                                        Ways to get context :




                                                                                        • getApplicationContext()

                                                                                        • getContext()


                                                                                        • getBaseContext()enter image description hereenter image description here






                                                                                        share|improve this answer






























                                                                                          20
















                                                                                          • Context represents a handle to get environment data .


                                                                                          • Context class itself is declared as abstract, whose implementation is provided by the android OS.


                                                                                          • Context is like remote of a TV & channel's in the television are resources, services, etc.
                                                                                            enter image description here


                                                                                          What can you do with it ?




                                                                                          • Loading resource.

                                                                                          • Launching a new activity.

                                                                                          • Creating views.

                                                                                          • Obtaining system service.


                                                                                          Ways to get context :




                                                                                          • getApplicationContext()

                                                                                          • getContext()


                                                                                          • getBaseContext()enter image description hereenter image description here






                                                                                          share|improve this answer




























                                                                                            20












                                                                                            20








                                                                                            20









                                                                                            • Context represents a handle to get environment data .


                                                                                            • Context class itself is declared as abstract, whose implementation is provided by the android OS.


                                                                                            • Context is like remote of a TV & channel's in the television are resources, services, etc.
                                                                                              enter image description here


                                                                                            What can you do with it ?




                                                                                            • Loading resource.

                                                                                            • Launching a new activity.

                                                                                            • Creating views.

                                                                                            • Obtaining system service.


                                                                                            Ways to get context :




                                                                                            • getApplicationContext()

                                                                                            • getContext()


                                                                                            • getBaseContext()enter image description hereenter image description here






                                                                                            share|improve this answer

















                                                                                            • Context represents a handle to get environment data .


                                                                                            • Context class itself is declared as abstract, whose implementation is provided by the android OS.


                                                                                            • Context is like remote of a TV & channel's in the television are resources, services, etc.
                                                                                              enter image description here


                                                                                            What can you do with it ?




                                                                                            • Loading resource.

                                                                                            • Launching a new activity.

                                                                                            • Creating views.

                                                                                            • Obtaining system service.


                                                                                            Ways to get context :




                                                                                            • getApplicationContext()

                                                                                            • getContext()


                                                                                            • getBaseContext()enter image description hereenter image description here







                                                                                            share|improve this answer














                                                                                            share|improve this answer



                                                                                            share|improve this answer








                                                                                            edited Nov 27 '18 at 5:10









                                                                                            Nilesh Rathod

                                                                                            31.2k82956




                                                                                            31.2k82956










                                                                                            answered May 2 '17 at 10:27







                                                                                            user4813855






























                                                                                                16














                                                                                                Just putting it out there for newbies;



                                                                                                So First understand Word Context :



                                                                                                In english-lib. it means:




                                                                                                "The circumstances that form the setting for an event, statement, or
                                                                                                idea, and in terms of which it can be fully understood and assessed."



                                                                                                "The parts of something written or spoken that immediately precede and
                                                                                                follow a word or passage and clarify its meaning."




                                                                                                Now take the same understanding to programming world:



                                                                                                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, package/application)



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



                                                                                                To Get Context Anywhere in application use following code:



                                                                                                Create new class AppContext inside your android application



                                                                                                public class AppContext extends Application {

                                                                                                private static Context context;

                                                                                                public void onCreate(){
                                                                                                super.onCreate();
                                                                                                AppContext.context = getApplicationContext();
                                                                                                }

                                                                                                public static Context getAppContext() {
                                                                                                return AppContext.context;
                                                                                                }
                                                                                                }


                                                                                                Now any time you want application context in non-activity class, call this method and you have application context.



                                                                                                Hope this help ;)






                                                                                                share|improve this answer






























                                                                                                  16














                                                                                                  Just putting it out there for newbies;



                                                                                                  So First understand Word Context :



                                                                                                  In english-lib. it means:




                                                                                                  "The circumstances that form the setting for an event, statement, or
                                                                                                  idea, and in terms of which it can be fully understood and assessed."



                                                                                                  "The parts of something written or spoken that immediately precede and
                                                                                                  follow a word or passage and clarify its meaning."




                                                                                                  Now take the same understanding to programming world:



                                                                                                  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, package/application)



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



                                                                                                  To Get Context Anywhere in application use following code:



                                                                                                  Create new class AppContext inside your android application



                                                                                                  public class AppContext extends Application {

                                                                                                  private static Context context;

                                                                                                  public void onCreate(){
                                                                                                  super.onCreate();
                                                                                                  AppContext.context = getApplicationContext();
                                                                                                  }

                                                                                                  public static Context getAppContext() {
                                                                                                  return AppContext.context;
                                                                                                  }
                                                                                                  }


                                                                                                  Now any time you want application context in non-activity class, call this method and you have application context.



                                                                                                  Hope this help ;)






                                                                                                  share|improve this answer




























                                                                                                    16












                                                                                                    16








                                                                                                    16







                                                                                                    Just putting it out there for newbies;



                                                                                                    So First understand Word Context :



                                                                                                    In english-lib. it means:




                                                                                                    "The circumstances that form the setting for an event, statement, or
                                                                                                    idea, and in terms of which it can be fully understood and assessed."



                                                                                                    "The parts of something written or spoken that immediately precede and
                                                                                                    follow a word or passage and clarify its meaning."




                                                                                                    Now take the same understanding to programming world:



                                                                                                    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, package/application)



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



                                                                                                    To Get Context Anywhere in application use following code:



                                                                                                    Create new class AppContext inside your android application



                                                                                                    public class AppContext extends Application {

                                                                                                    private static Context context;

                                                                                                    public void onCreate(){
                                                                                                    super.onCreate();
                                                                                                    AppContext.context = getApplicationContext();
                                                                                                    }

                                                                                                    public static Context getAppContext() {
                                                                                                    return AppContext.context;
                                                                                                    }
                                                                                                    }


                                                                                                    Now any time you want application context in non-activity class, call this method and you have application context.



                                                                                                    Hope this help ;)






                                                                                                    share|improve this answer















                                                                                                    Just putting it out there for newbies;



                                                                                                    So First understand Word Context :



                                                                                                    In english-lib. it means:




                                                                                                    "The circumstances that form the setting for an event, statement, or
                                                                                                    idea, and in terms of which it can be fully understood and assessed."



                                                                                                    "The parts of something written or spoken that immediately precede and
                                                                                                    follow a word or passage and clarify its meaning."




                                                                                                    Now take the same understanding to programming world:



                                                                                                    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, package/application)



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



                                                                                                    To Get Context Anywhere in application use following code:



                                                                                                    Create new class AppContext inside your android application



                                                                                                    public class AppContext extends Application {

                                                                                                    private static Context context;

                                                                                                    public void onCreate(){
                                                                                                    super.onCreate();
                                                                                                    AppContext.context = getApplicationContext();
                                                                                                    }

                                                                                                    public static Context getAppContext() {
                                                                                                    return AppContext.context;
                                                                                                    }
                                                                                                    }


                                                                                                    Now any time you want application context in non-activity class, call this method and you have application context.



                                                                                                    Hope this help ;)







                                                                                                    share|improve this answer














                                                                                                    share|improve this answer



                                                                                                    share|improve this answer








                                                                                                    edited Jun 4 '13 at 23:41

























                                                                                                    answered May 9 '13 at 3:23









                                                                                                    star18bitstar18bit

                                                                                                    8,00423742




                                                                                                    8,00423742























                                                                                                        15














                                                                                                        Think of it as the VM that has siloed the process the app or service is running in. The siloed environment has access to a bunch of underlying system information and certain permitted resources. You need that context to get at those services.






                                                                                                        share|improve this answer






























                                                                                                          15














                                                                                                          Think of it as the VM that has siloed the process the app or service is running in. The siloed environment has access to a bunch of underlying system information and certain permitted resources. You need that context to get at those services.






                                                                                                          share|improve this answer




























                                                                                                            15












                                                                                                            15








                                                                                                            15







                                                                                                            Think of it as the VM that has siloed the process the app or service is running in. The siloed environment has access to a bunch of underlying system information and certain permitted resources. You need that context to get at those services.






                                                                                                            share|improve this answer















                                                                                                            Think of it as the VM that has siloed the process the app or service is running in. The siloed environment has access to a bunch of underlying system information and certain permitted resources. You need that context to get at those services.







                                                                                                            share|improve this answer














                                                                                                            share|improve this answer



                                                                                                            share|improve this answer








                                                                                                            edited Sep 6 '12 at 20:26

























                                                                                                            answered Sep 6 '12 at 20:17









                                                                                                            ErolErol

                                                                                                            15115




                                                                                                            15115























                                                                                                                14














                                                                                                                Context is a reference to the current object as this. Also context allows access to information about the application environment.






                                                                                                                share|improve this answer






























                                                                                                                  14














                                                                                                                  Context is a reference to the current object as this. Also context allows access to information about the application environment.






                                                                                                                  share|improve this answer




























                                                                                                                    14












                                                                                                                    14








                                                                                                                    14







                                                                                                                    Context is a reference to the current object as this. Also context allows access to information about the application environment.






                                                                                                                    share|improve this answer















                                                                                                                    Context is a reference to the current object as this. Also context allows access to information about the application environment.







                                                                                                                    share|improve this answer














                                                                                                                    share|improve this answer



                                                                                                                    share|improve this answer








                                                                                                                    edited Apr 19 '16 at 18:59









                                                                                                                    Peter Mortensen

                                                                                                                    13.6k1984111




                                                                                                                    13.6k1984111










                                                                                                                    answered Jul 29 '11 at 6:17









                                                                                                                    devdasdevdas

                                                                                                                    14113




                                                                                                                    14113























                                                                                                                        11














                                                                                                                        The class android.content.Context provides the connection to the Android system and the resources of the project. It is the interface to global information about the application environment.



                                                                                                                        The Context also provides access to Android Services, e.g. the Location Service.



                                                                                                                        Activities and Services extend the Context class.






                                                                                                                        share|improve this answer






























                                                                                                                          11














                                                                                                                          The class android.content.Context provides the connection to the Android system and the resources of the project. It is the interface to global information about the application environment.



                                                                                                                          The Context also provides access to Android Services, e.g. the Location Service.



                                                                                                                          Activities and Services extend the Context class.






                                                                                                                          share|improve this answer




























                                                                                                                            11












                                                                                                                            11








                                                                                                                            11







                                                                                                                            The class android.content.Context provides the connection to the Android system and the resources of the project. It is the interface to global information about the application environment.



                                                                                                                            The Context also provides access to Android Services, e.g. the Location Service.



                                                                                                                            Activities and Services extend the Context class.






                                                                                                                            share|improve this answer















                                                                                                                            The class android.content.Context provides the connection to the Android system and the resources of the project. It is the interface to global information about the application environment.



                                                                                                                            The Context also provides access to Android Services, e.g. the Location Service.



                                                                                                                            Activities and Services extend the Context class.







                                                                                                                            share|improve this answer














                                                                                                                            share|improve this answer



                                                                                                                            share|improve this answer








                                                                                                                            edited Sep 14 '16 at 3:53









                                                                                                                            Vasily Kabunov

                                                                                                                            3,645113441




                                                                                                                            3,645113441










                                                                                                                            answered Nov 1 '12 at 19:01









                                                                                                                            Yashvir yadavYashvir yadav

                                                                                                                            11112




                                                                                                                            11112























                                                                                                                                7














                                                                                                                                Context is basically for resource access and getting the environment details of the application(for application context) or activity (for activity context) or any other...



                                                                                                                                In order to avoid memory leak you should use application context for every components that needs a context object.... for more click here






                                                                                                                                share|improve this answer




























                                                                                                                                  7














                                                                                                                                  Context is basically for resource access and getting the environment details of the application(for application context) or activity (for activity context) or any other...



                                                                                                                                  In order to avoid memory leak you should use application context for every components that needs a context object.... for more click here






                                                                                                                                  share|improve this answer


























                                                                                                                                    7












                                                                                                                                    7








                                                                                                                                    7







                                                                                                                                    Context is basically for resource access and getting the environment details of the application(for application context) or activity (for activity context) or any other...



                                                                                                                                    In order to avoid memory leak you should use application context for every components that needs a context object.... for more click here






                                                                                                                                    share|improve this answer













                                                                                                                                    Context is basically for resource access and getting the environment details of the application(for application context) or activity (for activity context) or any other...



                                                                                                                                    In order to avoid memory leak you should use application context for every components that needs a context object.... for more click here







                                                                                                                                    share|improve this answer












                                                                                                                                    share|improve this answer



                                                                                                                                    share|improve this answer










                                                                                                                                    answered Nov 16 '11 at 5:59









                                                                                                                                    Jaber ShabeekJaber Shabeek

                                                                                                                                    158212




                                                                                                                                    158212























                                                                                                                                        7














                                                                                                                                        Context is context of current state of the application/object.Its an entity that represents various environment data . Context helps the current activity to interact with out side android environment like local files, databases, class loaders associated to the environment, services including system-level services, and more.



                                                                                                                                        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. It’s like a handle to the environment your application is currently running in. The activity object inherits the Context object.



                                                                                                                                        Different invoking methods by which you can get context
                                                                                                                                        1. getApplicationContext(),
                                                                                                                                        2. getContext(),
                                                                                                                                        3. getBaseContext()
                                                                                                                                        4. or this (when in the activity class).






                                                                                                                                        share|improve this answer




























                                                                                                                                          7














                                                                                                                                          Context is context of current state of the application/object.Its an entity that represents various environment data . Context helps the current activity to interact with out side android environment like local files, databases, class loaders associated to the environment, services including system-level services, and more.



                                                                                                                                          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. It’s like a handle to the environment your application is currently running in. The activity object inherits the Context object.



                                                                                                                                          Different invoking methods by which you can get context
                                                                                                                                          1. getApplicationContext(),
                                                                                                                                          2. getContext(),
                                                                                                                                          3. getBaseContext()
                                                                                                                                          4. or this (when in the activity class).






                                                                                                                                          share|improve this answer


























                                                                                                                                            7












                                                                                                                                            7








                                                                                                                                            7







                                                                                                                                            Context is context of current state of the application/object.Its an entity that represents various environment data . Context helps the current activity to interact with out side android environment like local files, databases, class loaders associated to the environment, services including system-level services, and more.



                                                                                                                                            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. It’s like a handle to the environment your application is currently running in. The activity object inherits the Context object.



                                                                                                                                            Different invoking methods by which you can get context
                                                                                                                                            1. getApplicationContext(),
                                                                                                                                            2. getContext(),
                                                                                                                                            3. getBaseContext()
                                                                                                                                            4. or this (when in the activity class).






                                                                                                                                            share|improve this answer













                                                                                                                                            Context is context of current state of the application/object.Its an entity that represents various environment data . Context helps the current activity to interact with out side android environment like local files, databases, class loaders associated to the environment, services including system-level services, and more.



                                                                                                                                            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. It’s like a handle to the environment your application is currently running in. The activity object inherits the Context object.



                                                                                                                                            Different invoking methods by which you can get context
                                                                                                                                            1. getApplicationContext(),
                                                                                                                                            2. getContext(),
                                                                                                                                            3. getBaseContext()
                                                                                                                                            4. or this (when in the activity class).







                                                                                                                                            share|improve this answer












                                                                                                                                            share|improve this answer



                                                                                                                                            share|improve this answer










                                                                                                                                            answered Jan 4 '14 at 18:40









                                                                                                                                            santoshpatmcasantoshpatmca

                                                                                                                                            967199




                                                                                                                                            967199























                                                                                                                                                7














                                                                                                                                                Context is Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                                                                                                                                                It also gives access to the resources of the project. It is the interface to global information about the application environment.



                                                                                                                                                The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                                                                                                                                                Activities and services extend the Context class. Therefore they can be directly used to access the Context.






                                                                                                                                                share|improve this answer




























                                                                                                                                                  7














                                                                                                                                                  Context is Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                                                                                                                                                  It also gives access to the resources of the project. It is the interface to global information about the application environment.



                                                                                                                                                  The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                                                                                                                                                  Activities and services extend the Context class. Therefore they can be directly used to access the Context.






                                                                                                                                                  share|improve this answer


























                                                                                                                                                    7












                                                                                                                                                    7








                                                                                                                                                    7







                                                                                                                                                    Context is Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                                                                                                                                                    It also gives access to the resources of the project. It is the interface to global information about the application environment.



                                                                                                                                                    The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                                                                                                                                                    Activities and services extend the Context class. Therefore they can be directly used to access the Context.






                                                                                                                                                    share|improve this answer













                                                                                                                                                    Context is Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                                                                                                                                                    It also gives access to the resources of the project. It is the interface to global information about the application environment.



                                                                                                                                                    The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                                                                                                                                                    Activities and services extend the Context class. Therefore they can be directly used to access the Context.







                                                                                                                                                    share|improve this answer












                                                                                                                                                    share|improve this answer



                                                                                                                                                    share|improve this answer










                                                                                                                                                    answered Feb 6 '14 at 7:32









                                                                                                                                                    chandan kumarchandan kumar

                                                                                                                                                    7112




                                                                                                                                                    7112























                                                                                                                                                        7














                                                                                                                                                        Context is an interface to global information about an application environment. It's an abstract class whose implementation is provided by the Android system.



                                                                                                                                                        Context allows access to application-specific resources and classes, as well as calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.



                                                                                                                                                        Here is Example



                                                                                                                                                         public class MyActivity extends Activity {

                                                                                                                                                        public void Testing() {

                                                                                                                                                        Context actContext = this; /*returns the Activity Context since Activity extends Context.*/

                                                                                                                                                        Context appContext = getApplicationContext(); /*returns the context of the single, global Application object of the current process. */

                                                                                                                                                        Button BtnShowAct1 = (Button) findViewById(R.id.btnGoToAct1);
                                                                                                                                                        Context BtnContext = BtnShowAct1.getContext(); /*returns the context of the View. */


                                                                                                                                                        For more details you can visit http://developer.android.com/reference/android/content/Context.html






                                                                                                                                                        share|improve this answer




























                                                                                                                                                          7














                                                                                                                                                          Context is an interface to global information about an application environment. It's an abstract class whose implementation is provided by the Android system.



                                                                                                                                                          Context allows access to application-specific resources and classes, as well as calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.



                                                                                                                                                          Here is Example



                                                                                                                                                           public class MyActivity extends Activity {

                                                                                                                                                          public void Testing() {

                                                                                                                                                          Context actContext = this; /*returns the Activity Context since Activity extends Context.*/

                                                                                                                                                          Context appContext = getApplicationContext(); /*returns the context of the single, global Application object of the current process. */

                                                                                                                                                          Button BtnShowAct1 = (Button) findViewById(R.id.btnGoToAct1);
                                                                                                                                                          Context BtnContext = BtnShowAct1.getContext(); /*returns the context of the View. */


                                                                                                                                                          For more details you can visit http://developer.android.com/reference/android/content/Context.html






                                                                                                                                                          share|improve this answer


























                                                                                                                                                            7












                                                                                                                                                            7








                                                                                                                                                            7







                                                                                                                                                            Context is an interface to global information about an application environment. It's an abstract class whose implementation is provided by the Android system.



                                                                                                                                                            Context allows access to application-specific resources and classes, as well as calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.



                                                                                                                                                            Here is Example



                                                                                                                                                             public class MyActivity extends Activity {

                                                                                                                                                            public void Testing() {

                                                                                                                                                            Context actContext = this; /*returns the Activity Context since Activity extends Context.*/

                                                                                                                                                            Context appContext = getApplicationContext(); /*returns the context of the single, global Application object of the current process. */

                                                                                                                                                            Button BtnShowAct1 = (Button) findViewById(R.id.btnGoToAct1);
                                                                                                                                                            Context BtnContext = BtnShowAct1.getContext(); /*returns the context of the View. */


                                                                                                                                                            For more details you can visit http://developer.android.com/reference/android/content/Context.html






                                                                                                                                                            share|improve this answer













                                                                                                                                                            Context is an interface to global information about an application environment. It's an abstract class whose implementation is provided by the Android system.



                                                                                                                                                            Context allows access to application-specific resources and classes, as well as calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.



                                                                                                                                                            Here is Example



                                                                                                                                                             public class MyActivity extends Activity {

                                                                                                                                                            public void Testing() {

                                                                                                                                                            Context actContext = this; /*returns the Activity Context since Activity extends Context.*/

                                                                                                                                                            Context appContext = getApplicationContext(); /*returns the context of the single, global Application object of the current process. */

                                                                                                                                                            Button BtnShowAct1 = (Button) findViewById(R.id.btnGoToAct1);
                                                                                                                                                            Context BtnContext = BtnShowAct1.getContext(); /*returns the context of the View. */


                                                                                                                                                            For more details you can visit http://developer.android.com/reference/android/content/Context.html







                                                                                                                                                            share|improve this answer












                                                                                                                                                            share|improve this answer



                                                                                                                                                            share|improve this answer










                                                                                                                                                            answered Apr 26 '15 at 8:49









                                                                                                                                                            IntelliJ AmiyaIntelliJ Amiya

                                                                                                                                                            52.8k12112133




                                                                                                                                                            52.8k12112133























                                                                                                                                                                4














                                                                                                                                                                Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                                                                                                                                                                It also gives access to the resources of the project. It is the interface to global information about the application environment.



                                                                                                                                                                The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                                                                                                                                                                Activities and services extend the Context class. Therefore they can be directly used to access the Context.






                                                                                                                                                                share|improve this answer




























                                                                                                                                                                  4














                                                                                                                                                                  Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                                                                                                                                                                  It also gives access to the resources of the project. It is the interface to global information about the application environment.



                                                                                                                                                                  The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                                                                                                                                                                  Activities and services extend the Context class. Therefore they can be directly used to access the Context.






                                                                                                                                                                  share|improve this answer


























                                                                                                                                                                    4












                                                                                                                                                                    4








                                                                                                                                                                    4







                                                                                                                                                                    Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                                                                                                                                                                    It also gives access to the resources of the project. It is the interface to global information about the application environment.



                                                                                                                                                                    The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                                                                                                                                                                    Activities and services extend the Context class. Therefore they can be directly used to access the Context.






                                                                                                                                                                    share|improve this answer













                                                                                                                                                                    Instances of the the class android.content.Context provide the connection to the Android system which executes the application. For example, you can check the size of the current device display via the Context.



                                                                                                                                                                    It also gives access to the resources of the project. It is the interface to global information about the application environment.



                                                                                                                                                                    The Context class also provides access to Android services, e.g., the alarm manager to trigger time based events.



                                                                                                                                                                    Activities and services extend the Context class. Therefore they can be directly used to access the Context.







                                                                                                                                                                    share|improve this answer












                                                                                                                                                                    share|improve this answer



                                                                                                                                                                    share|improve this answer










                                                                                                                                                                    answered Feb 2 '14 at 11:33









                                                                                                                                                                    MubarakMubarak

                                                                                                                                                                    838916




                                                                                                                                                                    838916























                                                                                                                                                                        4














                                                                                                                                                                        A Context is what most of us would call Application. It's made by the Android system and is able to do only what an application is able to.
                                                                                                                                                                        In Tomcat, a Context is also what I would call an application.



                                                                                                                                                                        There is one Context that holds many Activities, each Activity may have many Views.



                                                                                                                                                                        Obviously, some will say that it doesn't fit because of this or that and they are probably right, but saying that a Context is your current application will help you to understand what you are putting in method parameters.






                                                                                                                                                                        share|improve this answer






























                                                                                                                                                                          4














                                                                                                                                                                          A Context is what most of us would call Application. It's made by the Android system and is able to do only what an application is able to.
                                                                                                                                                                          In Tomcat, a Context is also what I would call an application.



                                                                                                                                                                          There is one Context that holds many Activities, each Activity may have many Views.



                                                                                                                                                                          Obviously, some will say that it doesn't fit because of this or that and they are probably right, but saying that a Context is your current application will help you to understand what you are putting in method parameters.






                                                                                                                                                                          share|improve this answer




























                                                                                                                                                                            4












                                                                                                                                                                            4








                                                                                                                                                                            4







                                                                                                                                                                            A Context is what most of us would call Application. It's made by the Android system and is able to do only what an application is able to.
                                                                                                                                                                            In Tomcat, a Context is also what I would call an application.



                                                                                                                                                                            There is one Context that holds many Activities, each Activity may have many Views.



                                                                                                                                                                            Obviously, some will say that it doesn't fit because of this or that and they are probably right, but saying that a Context is your current application will help you to understand what you are putting in method parameters.






                                                                                                                                                                            share|improve this answer















                                                                                                                                                                            A Context is what most of us would call Application. It's made by the Android system and is able to do only what an application is able to.
                                                                                                                                                                            In Tomcat, a Context is also what I would call an application.



                                                                                                                                                                            There is one Context that holds many Activities, each Activity may have many Views.



                                                                                                                                                                            Obviously, some will say that it doesn't fit because of this or that and they are probably right, but saying that a Context is your current application will help you to understand what you are putting in method parameters.







                                                                                                                                                                            share|improve this answer














                                                                                                                                                                            share|improve this answer



                                                                                                                                                                            share|improve this answer








                                                                                                                                                                            edited Apr 19 '15 at 15:11

























                                                                                                                                                                            answered Oct 20 '13 at 8:00









                                                                                                                                                                            Nicolas ZozolNicolas Zozol

                                                                                                                                                                            4,93413959




                                                                                                                                                                            4,93413959























                                                                                                                                                                                4














                                                                                                                                                                                Boss Assistant Analogy



                                                                                                                                                                                Lets have a small analogy before diving deep in the technicality of Context




                                                                                                                                                                                Every Boss has an assistant or someone( errand boy) who does less
                                                                                                                                                                                important and more time-consuming things for him. For example, if they
                                                                                                                                                                                need a file or coffee then an assistant will be on run. Boss will not
                                                                                                                                                                                know what is going on in the background but the file or the task will
                                                                                                                                                                                be delivered



                                                                                                                                                                                So Here

                                                                                                                                                                                Boss - Android Application

                                                                                                                                                                                Assistant - Context

                                                                                                                                                                                File or cup of coffee - Resource




                                                                                                                                                                                What official Android Developer site says about Context



                                                                                                                                                                                Context is your access point for application related resources



                                                                                                                                                                                Let's see some of such resources or tasks




                                                                                                                                                                                • Launching an activity.


                                                                                                                                                                                • Getting absolute path to the application specific cache directory on
                                                                                                                                                                                  the filesystem.


                                                                                                                                                                                • Determining whether the given permission is allowed for a particular
                                                                                                                                                                                  process and user ID running in the system.


                                                                                                                                                                                • Checking whether you have been granted a particular permission.



                                                                                                                                                                                And so on.

                                                                                                                                                                                So if an Android application wants to start an activity, it goes straight to Context (Access Point), and the Context class gives him back the resources(Intent in this case).



                                                                                                                                                                                Like any other class Context class too has fields and methods.

                                                                                                                                                                                You can explore more about Context in official documentation, it covers pretty much everything, available methods, fields and even how to use fields with methods.






                                                                                                                                                                                share|improve this answer


























                                                                                                                                                                                • you've just copied an answer from below???

                                                                                                                                                                                  – Dilshad Abduwali
                                                                                                                                                                                  Sep 22 '17 at 6:11











                                                                                                                                                                                • thanks for heads up @dhssa . I was trying to provide the gist of Android docs .. Shoul've been more careful . Removed the "copied" part

                                                                                                                                                                                  – Rohit Singh
                                                                                                                                                                                  Sep 22 '17 at 6:56


















                                                                                                                                                                                4














                                                                                                                                                                                Boss Assistant Analogy



                                                                                                                                                                                Lets have a small analogy before diving deep in the technicality of Context




                                                                                                                                                                                Every Boss has an assistant or someone( errand boy) who does less
                                                                                                                                                                                important and more time-consuming things for him. For example, if they
                                                                                                                                                                                need a file or coffee then an assistant will be on run. Boss will not
                                                                                                                                                                                know what is going on in the background but the file or the task will
                                                                                                                                                                                be delivered



                                                                                                                                                                                So Here

                                                                                                                                                                                Boss - Android Application

                                                                                                                                                                                Assistant - Context

                                                                                                                                                                                File or cup of coffee - Resource




                                                                                                                                                                                What official Android Developer site says about Context



                                                                                                                                                                                Context is your access point for application related resources



                                                                                                                                                                                Let's see some of such resources or tasks




                                                                                                                                                                                • Launching an activity.


                                                                                                                                                                                • Getting absolute path to the application specific cache directory on
                                                                                                                                                                                  the filesystem.


                                                                                                                                                                                • Determining whether the given permission is allowed for a particular
                                                                                                                                                                                  process and user ID running in the system.


                                                                                                                                                                                • Checking whether you have been granted a particular permission.



                                                                                                                                                                                And so on.

                                                                                                                                                                                So if an Android application wants to start an activity, it goes straight to Context (Access Point), and the Context class gives him back the resources(Intent in this case).



                                                                                                                                                                                Like any other class Context class too has fields and methods.

                                                                                                                                                                                You can explore more about Context in official documentation, it covers pretty much everything, available methods, fields and even how to use fields with methods.






                                                                                                                                                                                share|improve this answer


























                                                                                                                                                                                • you've just copied an answer from below???

                                                                                                                                                                                  – Dilshad Abduwali
                                                                                                                                                                                  Sep 22 '17 at 6:11











                                                                                                                                                                                • thanks for heads up @dhssa . I was trying to provide the gist of Android docs .. Shoul've been more careful . Removed the "copied" part

                                                                                                                                                                                  – Rohit Singh
                                                                                                                                                                                  Sep 22 '17 at 6:56
















                                                                                                                                                                                4












                                                                                                                                                                                4








                                                                                                                                                                                4







                                                                                                                                                                                Boss Assistant Analogy



                                                                                                                                                                                Lets have a small analogy before diving deep in the technicality of Context




                                                                                                                                                                                Every Boss has an assistant or someone( errand boy) who does less
                                                                                                                                                                                important and more time-consuming things for him. For example, if they
                                                                                                                                                                                need a file or coffee then an assistant will be on run. Boss will not
                                                                                                                                                                                know what is going on in the background but the file or the task will
                                                                                                                                                                                be delivered



                                                                                                                                                                                So Here

                                                                                                                                                                                Boss - Android Application

                                                                                                                                                                                Assistant - Context

                                                                                                                                                                                File or cup of coffee - Resource




                                                                                                                                                                                What official Android Developer site says about Context



                                                                                                                                                                                Context is your access point for application related resources



                                                                                                                                                                                Let's see some of such resources or tasks




                                                                                                                                                                                • Launching an activity.


                                                                                                                                                                                • Getting absolute path to the application specific cache directory on
                                                                                                                                                                                  the filesystem.


                                                                                                                                                                                • Determining whether the given permission is allowed for a particular
                                                                                                                                                                                  process and user ID running in the system.


                                                                                                                                                                                • Checking whether you have been granted a particular permission.



                                                                                                                                                                                And so on.

                                                                                                                                                                                So if an Android application wants to start an activity, it goes straight to Context (Access Point), and the Context class gives him back the resources(Intent in this case).



                                                                                                                                                                                Like any other class Context class too has fields and methods.

                                                                                                                                                                                You can explore more about Context in official documentation, it covers pretty much everything, available methods, fields and even how to use fields with methods.






                                                                                                                                                                                share|improve this answer















                                                                                                                                                                                Boss Assistant Analogy



                                                                                                                                                                                Lets have a small analogy before diving deep in the technicality of Context




                                                                                                                                                                                Every Boss has an assistant or someone( errand boy) who does less
                                                                                                                                                                                important and more time-consuming things for him. For example, if they
                                                                                                                                                                                need a file or coffee then an assistant will be on run. Boss will not
                                                                                                                                                                                know what is going on in the background but the file or the task will
                                                                                                                                                                                be delivered



                                                                                                                                                                                So Here

                                                                                                                                                                                Boss - Android Application

                                                                                                                                                                                Assistant - Context

                                                                                                                                                                                File or cup of coffee - Resource




                                                                                                                                                                                What official Android Developer site says about Context



                                                                                                                                                                                Context is your access point for application related resources



                                                                                                                                                                                Let's see some of such resources or tasks




                                                                                                                                                                                • Launching an activity.


                                                                                                                                                                                • Getting absolute path to the application specific cache directory on
                                                                                                                                                                                  the filesystem.


                                                                                                                                                                                • Determining whether the given permission is allowed for a particular
                                                                                                                                                                                  process and user ID running in the system.


                                                                                                                                                                                • Checking whether you have been granted a particular permission.



                                                                                                                                                                                And so on.

                                                                                                                                                                                So if an Android application wants to start an activity, it goes straight to Context (Access Point), and the Context class gives him back the resources(Intent in this case).



                                                                                                                                                                                Like any other class Context class too has fields and methods.

                                                                                                                                                                                You can explore more about Context in official documentation, it covers pretty much everything, available methods, fields and even how to use fields with methods.







                                                                                                                                                                                share|improve this answer














                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                share|improve this answer








                                                                                                                                                                                edited Oct 5 '18 at 17:19

























                                                                                                                                                                                answered Sep 20 '17 at 6:51









                                                                                                                                                                                Rohit SinghRohit Singh

                                                                                                                                                                                2,79922733




                                                                                                                                                                                2,79922733













                                                                                                                                                                                • you've just copied an answer from below???

                                                                                                                                                                                  – Dilshad Abduwali
                                                                                                                                                                                  Sep 22 '17 at 6:11











                                                                                                                                                                                • thanks for heads up @dhssa . I was trying to provide the gist of Android docs .. Shoul've been more careful . Removed the "copied" part

                                                                                                                                                                                  – Rohit Singh
                                                                                                                                                                                  Sep 22 '17 at 6:56





















                                                                                                                                                                                • you've just copied an answer from below???

                                                                                                                                                                                  – Dilshad Abduwali
                                                                                                                                                                                  Sep 22 '17 at 6:11











                                                                                                                                                                                • thanks for heads up @dhssa . I was trying to provide the gist of Android docs .. Shoul've been more careful . Removed the "copied" part

                                                                                                                                                                                  – Rohit Singh
                                                                                                                                                                                  Sep 22 '17 at 6:56



















                                                                                                                                                                                you've just copied an answer from below???

                                                                                                                                                                                – Dilshad Abduwali
                                                                                                                                                                                Sep 22 '17 at 6:11





                                                                                                                                                                                you've just copied an answer from below???

                                                                                                                                                                                – Dilshad Abduwali
                                                                                                                                                                                Sep 22 '17 at 6:11













                                                                                                                                                                                thanks for heads up @dhssa . I was trying to provide the gist of Android docs .. Shoul've been more careful . Removed the "copied" part

                                                                                                                                                                                – Rohit Singh
                                                                                                                                                                                Sep 22 '17 at 6:56







                                                                                                                                                                                thanks for heads up @dhssa . I was trying to provide the gist of Android docs .. Shoul've been more careful . Removed the "copied" part

                                                                                                                                                                                – Rohit Singh
                                                                                                                                                                                Sep 22 '17 at 6:56













                                                                                                                                                                                3














                                                                                                                                                                                Context means Android get to know in which activity I should go for or act in.



                                                                                                                                                                                1 - Toast.makeText(context, "Enter All Details", Toast.LENGTH_SHORT).show();
                                                                                                                                                                                it used in this.
                                                                                                                                                                                Context context = ActivityName.this;



                                                                                                                                                                                2 -startActivity(new Intent(context,LoginActivity.class));



                                                                                                                                                                                in this context means from which activity you wanna go to other activity.
                                                                                                                                                                                context or ActivityName.this is faster then , getContext and getApplicatinContext.






                                                                                                                                                                                share|improve this answer




























                                                                                                                                                                                  3














                                                                                                                                                                                  Context means Android get to know in which activity I should go for or act in.



                                                                                                                                                                                  1 - Toast.makeText(context, "Enter All Details", Toast.LENGTH_SHORT).show();
                                                                                                                                                                                  it used in this.
                                                                                                                                                                                  Context context = ActivityName.this;



                                                                                                                                                                                  2 -startActivity(new Intent(context,LoginActivity.class));



                                                                                                                                                                                  in this context means from which activity you wanna go to other activity.
                                                                                                                                                                                  context or ActivityName.this is faster then , getContext and getApplicatinContext.






                                                                                                                                                                                  share|improve this answer


























                                                                                                                                                                                    3












                                                                                                                                                                                    3








                                                                                                                                                                                    3







                                                                                                                                                                                    Context means Android get to know in which activity I should go for or act in.



                                                                                                                                                                                    1 - Toast.makeText(context, "Enter All Details", Toast.LENGTH_SHORT).show();
                                                                                                                                                                                    it used in this.
                                                                                                                                                                                    Context context = ActivityName.this;



                                                                                                                                                                                    2 -startActivity(new Intent(context,LoginActivity.class));



                                                                                                                                                                                    in this context means from which activity you wanna go to other activity.
                                                                                                                                                                                    context or ActivityName.this is faster then , getContext and getApplicatinContext.






                                                                                                                                                                                    share|improve this answer













                                                                                                                                                                                    Context means Android get to know in which activity I should go for or act in.



                                                                                                                                                                                    1 - Toast.makeText(context, "Enter All Details", Toast.LENGTH_SHORT).show();
                                                                                                                                                                                    it used in this.
                                                                                                                                                                                    Context context = ActivityName.this;



                                                                                                                                                                                    2 -startActivity(new Intent(context,LoginActivity.class));



                                                                                                                                                                                    in this context means from which activity you wanna go to other activity.
                                                                                                                                                                                    context or ActivityName.this is faster then , getContext and getApplicatinContext.







                                                                                                                                                                                    share|improve this answer












                                                                                                                                                                                    share|improve this answer



                                                                                                                                                                                    share|improve this answer










                                                                                                                                                                                    answered Jun 24 '18 at 20:36









                                                                                                                                                                                    Deven ChavdaDeven Chavda

                                                                                                                                                                                    1135




                                                                                                                                                                                    1135























                                                                                                                                                                                        1














                                                                                                                                                                                        If you want to connect Context with other familiar classes in Android, keep in mind this structure:




                                                                                                                                                                                        Context < ContextWrapper < Application



                                                                                                                                                                                        Context < ContextWrapper < ContextThemeWrapper < Activity



                                                                                                                                                                                        Context < ContextWrapper < ContextThemeWrapper < Activity <
                                                                                                                                                                                        ListActivity



                                                                                                                                                                                        Context < ContextWrapper < Service



                                                                                                                                                                                        Context < ContextWrapper < Service < IntentService




                                                                                                                                                                                        So, all of those classes are contexts in their own way. You can cast Service and ListActivity to Context if you wish. But if you look closely, some of the classes inherit theme as well. In activity or fragment, you would like theming to be applied to your views, but don't care about it Service class, for instance.



                                                                                                                                                                                        I explain the difference in contexts here.






                                                                                                                                                                                        share|improve this answer




























                                                                                                                                                                                          1














                                                                                                                                                                                          If you want to connect Context with other familiar classes in Android, keep in mind this structure:




                                                                                                                                                                                          Context < ContextWrapper < Application



                                                                                                                                                                                          Context < ContextWrapper < ContextThemeWrapper < Activity



                                                                                                                                                                                          Context < ContextWrapper < ContextThemeWrapper < Activity <
                                                                                                                                                                                          ListActivity



                                                                                                                                                                                          Context < ContextWrapper < Service



                                                                                                                                                                                          Context < ContextWrapper < Service < IntentService




                                                                                                                                                                                          So, all of those classes are contexts in their own way. You can cast Service and ListActivity to Context if you wish. But if you look closely, some of the classes inherit theme as well. In activity or fragment, you would like theming to be applied to your views, but don't care about it Service class, for instance.



                                                                                                                                                                                          I explain the difference in contexts here.






                                                                                                                                                                                          share|improve this answer


























                                                                                                                                                                                            1












                                                                                                                                                                                            1








                                                                                                                                                                                            1







                                                                                                                                                                                            If you want to connect Context with other familiar classes in Android, keep in mind this structure:




                                                                                                                                                                                            Context < ContextWrapper < Application



                                                                                                                                                                                            Context < ContextWrapper < ContextThemeWrapper < Activity



                                                                                                                                                                                            Context < ContextWrapper < ContextThemeWrapper < Activity <
                                                                                                                                                                                            ListActivity



                                                                                                                                                                                            Context < ContextWrapper < Service



                                                                                                                                                                                            Context < ContextWrapper < Service < IntentService




                                                                                                                                                                                            So, all of those classes are contexts in their own way. You can cast Service and ListActivity to Context if you wish. But if you look closely, some of the classes inherit theme as well. In activity or fragment, you would like theming to be applied to your views, but don't care about it Service class, for instance.



                                                                                                                                                                                            I explain the difference in contexts here.






                                                                                                                                                                                            share|improve this answer













                                                                                                                                                                                            If you want to connect Context with other familiar classes in Android, keep in mind this structure:




                                                                                                                                                                                            Context < ContextWrapper < Application



                                                                                                                                                                                            Context < ContextWrapper < ContextThemeWrapper < Activity



                                                                                                                                                                                            Context < ContextWrapper < ContextThemeWrapper < Activity <
                                                                                                                                                                                            ListActivity



                                                                                                                                                                                            Context < ContextWrapper < Service



                                                                                                                                                                                            Context < ContextWrapper < Service < IntentService




                                                                                                                                                                                            So, all of those classes are contexts in their own way. You can cast Service and ListActivity to Context if you wish. But if you look closely, some of the classes inherit theme as well. In activity or fragment, you would like theming to be applied to your views, but don't care about it Service class, for instance.



                                                                                                                                                                                            I explain the difference in contexts here.







                                                                                                                                                                                            share|improve this answer












                                                                                                                                                                                            share|improve this answer



                                                                                                                                                                                            share|improve this answer










                                                                                                                                                                                            answered Feb 14 '17 at 10:08









                                                                                                                                                                                            lomzalomza

                                                                                                                                                                                            4,565135578




                                                                                                                                                                                            4,565135578























                                                                                                                                                                                                1














                                                                                                                                                                                                Putting simple, Androids Context is a mess that you won't love until you stop worrying about.



                                                                                                                                                                                                Android Contexts are:




                                                                                                                                                                                                • God-objects.



                                                                                                                                                                                                • Thing that you want to pass around all your application when you are starting developing for Android, but will avoid doing it when you get a little bit closer to programming, testing and Android itself.




                                                                                                                                                                                                  • Unclear dependency.


                                                                                                                                                                                                  • Common source of memory leaks.


                                                                                                                                                                                                  • PITA for testing.




                                                                                                                                                                                                • Actual context used by Android system to dispatch permissions, resources, preferences, services, broadcasts, styles, showing dialogs and inflating layout. And you need different Context instances for some separate things (obviously, you can't show a dialog from an application or service context; layouts inflated from application and activity contexts may differ).







                                                                                                                                                                                                share|improve this answer






























                                                                                                                                                                                                  1














                                                                                                                                                                                                  Putting simple, Androids Context is a mess that you won't love until you stop worrying about.



                                                                                                                                                                                                  Android Contexts are:




                                                                                                                                                                                                  • God-objects.



                                                                                                                                                                                                  • Thing that you want to pass around all your application when you are starting developing for Android, but will avoid doing it when you get a little bit closer to programming, testing and Android itself.




                                                                                                                                                                                                    • Unclear dependency.


                                                                                                                                                                                                    • Common source of memory leaks.


                                                                                                                                                                                                    • PITA for testing.




                                                                                                                                                                                                  • Actual context used by Android system to dispatch permissions, resources, preferences, services, broadcasts, styles, showing dialogs and inflating layout. And you need different Context instances for some separate things (obviously, you can't show a dialog from an application or service context; layouts inflated from application and activity contexts may differ).







                                                                                                                                                                                                  share|improve this answer




























                                                                                                                                                                                                    1












                                                                                                                                                                                                    1








                                                                                                                                                                                                    1







                                                                                                                                                                                                    Putting simple, Androids Context is a mess that you won't love until you stop worrying about.



                                                                                                                                                                                                    Android Contexts are:




                                                                                                                                                                                                    • God-objects.



                                                                                                                                                                                                    • Thing that you want to pass around all your application when you are starting developing for Android, but will avoid doing it when you get a little bit closer to programming, testing and Android itself.




                                                                                                                                                                                                      • Unclear dependency.


                                                                                                                                                                                                      • Common source of memory leaks.


                                                                                                                                                                                                      • PITA for testing.




                                                                                                                                                                                                    • Actual context used by Android system to dispatch permissions, resources, preferences, services, broadcasts, styles, showing dialogs and inflating layout. And you need different Context instances for some separate things (obviously, you can't show a dialog from an application or service context; layouts inflated from application and activity contexts may differ).







                                                                                                                                                                                                    share|improve this answer















                                                                                                                                                                                                    Putting simple, Androids Context is a mess that you won't love until you stop worrying about.



                                                                                                                                                                                                    Android Contexts are:




                                                                                                                                                                                                    • God-objects.



                                                                                                                                                                                                    • Thing that you want to pass around all your application when you are starting developing for Android, but will avoid doing it when you get a little bit closer to programming, testing and Android itself.




                                                                                                                                                                                                      • Unclear dependency.


                                                                                                                                                                                                      • Common source of memory leaks.


                                                                                                                                                                                                      • PITA for testing.




                                                                                                                                                                                                    • Actual context used by Android system to dispatch permissions, resources, preferences, services, broadcasts, styles, showing dialogs and inflating layout. And you need different Context instances for some separate things (obviously, you can't show a dialog from an application or service context; layouts inflated from application and activity contexts may differ).








                                                                                                                                                                                                    share|improve this answer














                                                                                                                                                                                                    share|improve this answer



                                                                                                                                                                                                    share|improve this answer








                                                                                                                                                                                                    edited Apr 2 '17 at 21:59

























                                                                                                                                                                                                    answered Apr 2 '17 at 21:45









                                                                                                                                                                                                    PavlusPavlus

                                                                                                                                                                                                    767516




                                                                                                                                                                                                    767516























                                                                                                                                                                                                        1














                                                                                                                                                                                                        The Context is the android specific api to each app-s Sandbox
                                                                                                                                                                                                        that provides access app private data like to resources, database, private filedirectories, preferences, settings ...



                                                                                                                                                                                                        Most of the privatedata are the same for all activities/services/broadcastlisteners of one application.



                                                                                                                                                                                                        Since Application, Activity, Service implement the Context interface they can be used where an api call needs a Context parameter






                                                                                                                                                                                                        share|improve this answer






























                                                                                                                                                                                                          1














                                                                                                                                                                                                          The Context is the android specific api to each app-s Sandbox
                                                                                                                                                                                                          that provides access app private data like to resources, database, private filedirectories, preferences, settings ...



                                                                                                                                                                                                          Most of the privatedata are the same for all activities/services/broadcastlisteners of one application.



                                                                                                                                                                                                          Since Application, Activity, Service implement the Context interface they can be used where an api call needs a Context parameter






                                                                                                                                                                                                          share|improve this answer




























                                                                                                                                                                                                            1












                                                                                                                                                                                                            1








                                                                                                                                                                                                            1







                                                                                                                                                                                                            The Context is the android specific api to each app-s Sandbox
                                                                                                                                                                                                            that provides access app private data like to resources, database, private filedirectories, preferences, settings ...



                                                                                                                                                                                                            Most of the privatedata are the same for all activities/services/broadcastlisteners of one application.



                                                                                                                                                                                                            Since Application, Activity, Service implement the Context interface they can be used where an api call needs a Context parameter






                                                                                                                                                                                                            share|improve this answer















                                                                                                                                                                                                            The Context is the android specific api to each app-s Sandbox
                                                                                                                                                                                                            that provides access app private data like to resources, database, private filedirectories, preferences, settings ...



                                                                                                                                                                                                            Most of the privatedata are the same for all activities/services/broadcastlisteners of one application.



                                                                                                                                                                                                            Since Application, Activity, Service implement the Context interface they can be used where an api call needs a Context parameter







                                                                                                                                                                                                            share|improve this answer














                                                                                                                                                                                                            share|improve this answer



                                                                                                                                                                                                            share|improve this answer








                                                                                                                                                                                                            edited Apr 12 '17 at 9:02

























                                                                                                                                                                                                            answered Mar 27 '17 at 16:33









                                                                                                                                                                                                            k3bk3b

                                                                                                                                                                                                            10.9k54169




                                                                                                                                                                                                            10.9k54169























                                                                                                                                                                                                                0














                                                                                                                                                                                                                Context means component (or application) in various time-period. If I do eat so many food between 1 to 2 pm then my context of that time is used to access all methods (or resources) that I use during that time. Content is an component (application) for particular time. Context of components of application keeps changing based on the underlying lifecycle of the components or application.
                                                                                                                                                                                                                For instance, inside the onCreate() of an Activity,



                                                                                                                                                                                                                getBaseContext() -- gives the context of the Activity that is set (created) by the constructor of activity.
                                                                                                                                                                                                                getApplicationContext() -- gives the Context setup (created) during the creation of application.



                                                                                                                                                                                                                Note: <application> holds all Android Components.



                                                                                                                                                                                                                <application>
                                                                                                                                                                                                                <activity> .. </activity>

                                                                                                                                                                                                                <service> .. </service>

                                                                                                                                                                                                                <receiver> .. </receiver>

                                                                                                                                                                                                                <provider> .. </provider>
                                                                                                                                                                                                                </application>


                                                                                                                                                                                                                It means, when you call getApplicationContext() from inside whatever component, you are calling the common context of the whole application.



                                                                                                                                                                                                                Context keeps being modified by the system based on the lifecycle of components.






                                                                                                                                                                                                                share|improve this answer






























                                                                                                                                                                                                                  0














                                                                                                                                                                                                                  Context means component (or application) in various time-period. If I do eat so many food between 1 to 2 pm then my context of that time is used to access all methods (or resources) that I use during that time. Content is an component (application) for particular time. Context of components of application keeps changing based on the underlying lifecycle of the components or application.
                                                                                                                                                                                                                  For instance, inside the onCreate() of an Activity,



                                                                                                                                                                                                                  getBaseContext() -- gives the context of the Activity that is set (created) by the constructor of activity.
                                                                                                                                                                                                                  getApplicationContext() -- gives the Context setup (created) during the creation of application.



                                                                                                                                                                                                                  Note: <application> holds all Android Components.



                                                                                                                                                                                                                  <application>
                                                                                                                                                                                                                  <activity> .. </activity>

                                                                                                                                                                                                                  <service> .. </service>

                                                                                                                                                                                                                  <receiver> .. </receiver>

                                                                                                                                                                                                                  <provider> .. </provider>
                                                                                                                                                                                                                  </application>


                                                                                                                                                                                                                  It means, when you call getApplicationContext() from inside whatever component, you are calling the common context of the whole application.



                                                                                                                                                                                                                  Context keeps being modified by the system based on the lifecycle of components.






                                                                                                                                                                                                                  share|improve this answer




























                                                                                                                                                                                                                    0












                                                                                                                                                                                                                    0








                                                                                                                                                                                                                    0







                                                                                                                                                                                                                    Context means component (or application) in various time-period. If I do eat so many food between 1 to 2 pm then my context of that time is used to access all methods (or resources) that I use during that time. Content is an component (application) for particular time. Context of components of application keeps changing based on the underlying lifecycle of the components or application.
                                                                                                                                                                                                                    For instance, inside the onCreate() of an Activity,



                                                                                                                                                                                                                    getBaseContext() -- gives the context of the Activity that is set (created) by the constructor of activity.
                                                                                                                                                                                                                    getApplicationContext() -- gives the Context setup (created) during the creation of application.



                                                                                                                                                                                                                    Note: <application> holds all Android Components.



                                                                                                                                                                                                                    <application>
                                                                                                                                                                                                                    <activity> .. </activity>

                                                                                                                                                                                                                    <service> .. </service>

                                                                                                                                                                                                                    <receiver> .. </receiver>

                                                                                                                                                                                                                    <provider> .. </provider>
                                                                                                                                                                                                                    </application>


                                                                                                                                                                                                                    It means, when you call getApplicationContext() from inside whatever component, you are calling the common context of the whole application.



                                                                                                                                                                                                                    Context keeps being modified by the system based on the lifecycle of components.






                                                                                                                                                                                                                    share|improve this answer















                                                                                                                                                                                                                    Context means component (or application) in various time-period. If I do eat so many food between 1 to 2 pm then my context of that time is used to access all methods (or resources) that I use during that time. Content is an component (application) for particular time. Context of components of application keeps changing based on the underlying lifecycle of the components or application.
                                                                                                                                                                                                                    For instance, inside the onCreate() of an Activity,



                                                                                                                                                                                                                    getBaseContext() -- gives the context of the Activity that is set (created) by the constructor of activity.
                                                                                                                                                                                                                    getApplicationContext() -- gives the Context setup (created) during the creation of application.



                                                                                                                                                                                                                    Note: <application> holds all Android Components.



                                                                                                                                                                                                                    <application>
                                                                                                                                                                                                                    <activity> .. </activity>

                                                                                                                                                                                                                    <service> .. </service>

                                                                                                                                                                                                                    <receiver> .. </receiver>

                                                                                                                                                                                                                    <provider> .. </provider>
                                                                                                                                                                                                                    </application>


                                                                                                                                                                                                                    It means, when you call getApplicationContext() from inside whatever component, you are calling the common context of the whole application.



                                                                                                                                                                                                                    Context keeps being modified by the system based on the lifecycle of components.







                                                                                                                                                                                                                    share|improve this answer














                                                                                                                                                                                                                    share|improve this answer



                                                                                                                                                                                                                    share|improve this answer








                                                                                                                                                                                                                    edited Mar 29 '18 at 14:48









                                                                                                                                                                                                                    dentex

                                                                                                                                                                                                                    2,39032041




                                                                                                                                                                                                                    2,39032041










                                                                                                                                                                                                                    answered Oct 10 '17 at 16:54









                                                                                                                                                                                                                    Uddhav GautamUddhav Gautam

                                                                                                                                                                                                                    3,45721739




                                                                                                                                                                                                                    3,45721739























                                                                                                                                                                                                                        0














                                                                                                                                                                                                                         Context means current.
                                                                                                                                                                                                                        Context use to do operation for current screen.
                                                                                                                                                                                                                        ex.

                                                                                                                                                                                                                          1. getApplicationContext()

                                                                                                                                                                                                                          2. getContext()



                                                                                                                                                                                                                        Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();





                                                                                                                                                                                                                        share|improve this answer






























                                                                                                                                                                                                                          0














                                                                                                                                                                                                                           Context means current.
                                                                                                                                                                                                                          Context use to do operation for current screen.
                                                                                                                                                                                                                          ex.

                                                                                                                                                                                                                            1. getApplicationContext()

                                                                                                                                                                                                                            2. getContext()



                                                                                                                                                                                                                          Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();





                                                                                                                                                                                                                          share|improve this answer




























                                                                                                                                                                                                                            0












                                                                                                                                                                                                                            0








                                                                                                                                                                                                                            0







                                                                                                                                                                                                                             Context means current.
                                                                                                                                                                                                                            Context use to do operation for current screen.
                                                                                                                                                                                                                            ex.

                                                                                                                                                                                                                              1. getApplicationContext()

                                                                                                                                                                                                                              2. getContext()



                                                                                                                                                                                                                            Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();





                                                                                                                                                                                                                            share|improve this answer















                                                                                                                                                                                                                             Context means current.
                                                                                                                                                                                                                            Context use to do operation for current screen.
                                                                                                                                                                                                                            ex.

                                                                                                                                                                                                                              1. getApplicationContext()

                                                                                                                                                                                                                              2. getContext()



                                                                                                                                                                                                                            Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();






                                                                                                                                                                                                                            share|improve this answer














                                                                                                                                                                                                                            share|improve this answer



                                                                                                                                                                                                                            share|improve this answer








                                                                                                                                                                                                                            edited Mar 29 '18 at 16:16









                                                                                                                                                                                                                            RKURBAN

                                                                                                                                                                                                                            1298




                                                                                                                                                                                                                            1298










                                                                                                                                                                                                                            answered May 1 '17 at 13:02









                                                                                                                                                                                                                            Nikita PrajapatiNikita Prajapati

                                                                                                                                                                                                                            754




                                                                                                                                                                                                                            754























                                                                                                                                                                                                                                -6














                                                                                                                                                                                                                                This attribute declares which activity this layout is associated with by default.






                                                                                                                                                                                                                                share|improve this answer




























                                                                                                                                                                                                                                  -6














                                                                                                                                                                                                                                  This attribute declares which activity this layout is associated with by default.






                                                                                                                                                                                                                                  share|improve this answer


























                                                                                                                                                                                                                                    -6












                                                                                                                                                                                                                                    -6








                                                                                                                                                                                                                                    -6







                                                                                                                                                                                                                                    This attribute declares which activity this layout is associated with by default.






                                                                                                                                                                                                                                    share|improve this answer













                                                                                                                                                                                                                                    This attribute declares which activity this layout is associated with by default.







                                                                                                                                                                                                                                    share|improve this answer












                                                                                                                                                                                                                                    share|improve this answer



                                                                                                                                                                                                                                    share|improve this answer










                                                                                                                                                                                                                                    answered Jul 2 '17 at 2:06









                                                                                                                                                                                                                                    saleem kalrosaleem kalro

                                                                                                                                                                                                                                    49335




                                                                                                                                                                                                                                    49335























                                                                                                                                                                                                                                        -6














                                                                                                                                                                                                                                        In Java, we say this keyword refers to the state of the current object of the application.


                                                                                                                                                                                                                                        Similarly, in an alternate we have Context in Android Development.



                                                                                                                                                                                                                                        This can be defined either explicitly or implicitly,



                                                                                                                                                                                                                                        Context con = this;

                                                                                                                                                                                                                                        getApplicationContext();

                                                                                                                                                                                                                                        getBaseContext();

                                                                                                                                                                                                                                        getContext();





                                                                                                                                                                                                                                        share|improve this answer


























                                                                                                                                                                                                                                        • Could be just the object of the current activity I think. I use it as if it were this way a few times.

                                                                                                                                                                                                                                          – Ohiovr
                                                                                                                                                                                                                                          Dec 27 '16 at 0:56






                                                                                                                                                                                                                                        • 1





                                                                                                                                                                                                                                          There is a large variety of ways to get context. Calling this only works in activities. Context isn't an alternative to "this" - context is used to access system features, and a lot more. You are over-simplifying Context and missing the main point with it.

                                                                                                                                                                                                                                          – Zoe
                                                                                                                                                                                                                                          Jul 14 '17 at 15:18
















                                                                                                                                                                                                                                        -6














                                                                                                                                                                                                                                        In Java, we say this keyword refers to the state of the current object of the application.


                                                                                                                                                                                                                                        Similarly, in an alternate we have Context in Android Development.



                                                                                                                                                                                                                                        This can be defined either explicitly or implicitly,



                                                                                                                                                                                                                                        Context con = this;

                                                                                                                                                                                                                                        getApplicationContext();

                                                                                                                                                                                                                                        getBaseContext();

                                                                                                                                                                                                                                        getContext();





                                                                                                                                                                                                                                        share|improve this answer


























                                                                                                                                                                                                                                        • Could be just the object of the current activity I think. I use it as if it were this way a few times.

                                                                                                                                                                                                                                          – Ohiovr
                                                                                                                                                                                                                                          Dec 27 '16 at 0:56






                                                                                                                                                                                                                                        • 1





                                                                                                                                                                                                                                          There is a large variety of ways to get context. Calling this only works in activities. Context isn't an alternative to "this" - context is used to access system features, and a lot more. You are over-simplifying Context and missing the main point with it.

                                                                                                                                                                                                                                          – Zoe
                                                                                                                                                                                                                                          Jul 14 '17 at 15:18














                                                                                                                                                                                                                                        -6












                                                                                                                                                                                                                                        -6








                                                                                                                                                                                                                                        -6







                                                                                                                                                                                                                                        In Java, we say this keyword refers to the state of the current object of the application.


                                                                                                                                                                                                                                        Similarly, in an alternate we have Context in Android Development.



                                                                                                                                                                                                                                        This can be defined either explicitly or implicitly,



                                                                                                                                                                                                                                        Context con = this;

                                                                                                                                                                                                                                        getApplicationContext();

                                                                                                                                                                                                                                        getBaseContext();

                                                                                                                                                                                                                                        getContext();





                                                                                                                                                                                                                                        share|improve this answer















                                                                                                                                                                                                                                        In Java, we say this keyword refers to the state of the current object of the application.


                                                                                                                                                                                                                                        Similarly, in an alternate we have Context in Android Development.



                                                                                                                                                                                                                                        This can be defined either explicitly or implicitly,



                                                                                                                                                                                                                                        Context con = this;

                                                                                                                                                                                                                                        getApplicationContext();

                                                                                                                                                                                                                                        getBaseContext();

                                                                                                                                                                                                                                        getContext();






                                                                                                                                                                                                                                        share|improve this answer














                                                                                                                                                                                                                                        share|improve this answer



                                                                                                                                                                                                                                        share|improve this answer








                                                                                                                                                                                                                                        edited Mar 29 '18 at 15:37









                                                                                                                                                                                                                                        RKURBAN

                                                                                                                                                                                                                                        1298




                                                                                                                                                                                                                                        1298










                                                                                                                                                                                                                                        answered Jun 23 '16 at 2:00









                                                                                                                                                                                                                                        Gavine JoyceGavine Joyce

                                                                                                                                                                                                                                        24337




                                                                                                                                                                                                                                        24337













                                                                                                                                                                                                                                        • Could be just the object of the current activity I think. I use it as if it were this way a few times.

                                                                                                                                                                                                                                          – Ohiovr
                                                                                                                                                                                                                                          Dec 27 '16 at 0:56






                                                                                                                                                                                                                                        • 1





                                                                                                                                                                                                                                          There is a large variety of ways to get context. Calling this only works in activities. Context isn't an alternative to "this" - context is used to access system features, and a lot more. You are over-simplifying Context and missing the main point with it.

                                                                                                                                                                                                                                          – Zoe
                                                                                                                                                                                                                                          Jul 14 '17 at 15:18



















                                                                                                                                                                                                                                        • Could be just the object of the current activity I think. I use it as if it were this way a few times.

                                                                                                                                                                                                                                          – Ohiovr
                                                                                                                                                                                                                                          Dec 27 '16 at 0:56






                                                                                                                                                                                                                                        • 1





                                                                                                                                                                                                                                          There is a large variety of ways to get context. Calling this only works in activities. Context isn't an alternative to "this" - context is used to access system features, and a lot more. You are over-simplifying Context and missing the main point with it.

                                                                                                                                                                                                                                          – Zoe
                                                                                                                                                                                                                                          Jul 14 '17 at 15:18

















                                                                                                                                                                                                                                        Could be just the object of the current activity I think. I use it as if it were this way a few times.

                                                                                                                                                                                                                                        – Ohiovr
                                                                                                                                                                                                                                        Dec 27 '16 at 0:56





                                                                                                                                                                                                                                        Could be just the object of the current activity I think. I use it as if it were this way a few times.

                                                                                                                                                                                                                                        – Ohiovr
                                                                                                                                                                                                                                        Dec 27 '16 at 0:56




                                                                                                                                                                                                                                        1




                                                                                                                                                                                                                                        1





                                                                                                                                                                                                                                        There is a large variety of ways to get context. Calling this only works in activities. Context isn't an alternative to "this" - context is used to access system features, and a lot more. You are over-simplifying Context and missing the main point with it.

                                                                                                                                                                                                                                        – Zoe
                                                                                                                                                                                                                                        Jul 14 '17 at 15:18





                                                                                                                                                                                                                                        There is a large variety of ways to get context. Calling this only works in activities. Context isn't an alternative to "this" - context is used to access system features, and a lot more. You are over-simplifying Context and missing the main point with it.

                                                                                                                                                                                                                                        – Zoe
                                                                                                                                                                                                                                        Jul 14 '17 at 15:18



                                                                                                                                                                                                                                        Z6JVp2NcSs QANdxMR,mJrGuTzDK9G,8 aRqCVZou30Q5ph3HrTA 8o2S0
                                                                                                                                                                                                                                        Ua HWdF6vxH GqUrw,lClEtMNNFjvP6f,cn,x37rsI6I6yikgLfFL Xzke JKh RuHwDGAOFTwm1c5dP278nH YFVUHdea7w I

                                                                                                                                                                                                                                        Popular posts from this blog

                                                                                                                                                                                                                                        Monofisismo

                                                                                                                                                                                                                                        Angular Downloading a file using contenturl with Basic Authentication

                                                                                                                                                                                                                                        Olmecas