What is 'Context' on Android?

前端 未结 30 2788
南旧
南旧 2020-11-21 04:46

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

相关标签:
30条回答
  • 2020-11-21 05:04

    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).

    0 讨论(0)
  • 2020-11-21 05:04

    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).

    0 讨论(0)
  • 2020-11-21 05:05

    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

    0 讨论(0)
  • 2020-11-21 05:06

    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.

    0 讨论(0)
  • 2020-11-21 05:08

    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.

    0 讨论(0)
  • 2020-11-21 05:08

    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.

    0 讨论(0)
提交回复
热议问题