What is 'Context' on Android?

前端 未结 30 3015
南旧
南旧 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:21

    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.

    0 讨论(0)
  • 2020-11-21 05:23
    • 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.

    What can you do with it ?

    • Loading resource.
    • Launching a new activity.
    • Creating views.
    • Obtaining system service.

    Ways to get context :

    • getApplicationContext()
    • getContext()
    • getBaseContext()
    0 讨论(0)
  • 2020-11-21 05:23

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

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

    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 to 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 has 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 the 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);
    

    The keyword this refers to the context of the current activity.

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

    The Context in Android is actually the context of what we are talking about and where we are currently present. This will become more clear as we go along with this.

    Few important points about the context:

    • It is the context of the current state of the application.
    • It can be used to get information regarding the activity and application.
    • It can be used to get access to resources, databases, and shared preferences, and etc.
    • Both the Activity and Application classes extend the Context class.

    Context is almost everywhere in Android Development and it is the most important thing in Android Development, so we must understand to use it correctly.

    Wrong use of Context can easily lead to memory leaks in an android application.

    As there are different types of context in Android, we as an Android Developer often get confused about which context to use at which place. So let’s understand what are those, how to use those, and when to use which one.

    Mainly two types of context:

    • Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only.
    • Activity Context: It is the activity and we are present in Activity. For example - MainActivity. It is an instance of MainActivity only.

    Application Context

    It is an instance that is the singleton and can be accessed in activity via getApplicationContext(). This context is tied to the lifecycle of an application. The application context can be used where you need a context whose lifecycle is separate from the current context or when you are passing a context beyond the scope of activity.

    Example Use: If you have to create a singleton object for your application and that object needs a context, always pass the application context.

    If you pass the activity context here, it will lead to the memory leak as it will keep the reference to the activity and activity will not be garbage collected.

    In case, when you have to initialize a library in an activity, always pass the application context, not the activity context.

    You only use getApplicationContext() when you know you need a Context for something that may live longer than any other likely Context you have at your disposal.

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

    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.

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