Android: Why Must Use Getbasecontext() Instead of This

Android: why must use getBaseContext() instead of this

  1. getApplicationContext () returns the application context of the entire application life cycle,when application will destroy then it will destroy also.

  2. this the context returns the current context of the activity, belong to the activity, the activity is destroyed then it will destroy also.but in your case it will refers to the Spinner instance because we are using this within onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3) method which is from Spinner class and Spinner inherit this method from AdapterView.OnItemSelectedListener interface

  3. getBaseContext() is the method of ContextWrapper. And ContextWrapper is, "Proxying implementation of Context that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context." (as per javadocs)..

and in your case :Spinner class is not subclass of Context or ContextWrapper class*

Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show();

means getBaseContext() is method of ContextWrapper and ContextWrapper is Proxying implementation of Context so indirectly we are passing an Context Class Object.

or we can also pass 'Activity.this' because Activity class is subclass of ContextWrapper class .

if you go with android documention then this method require an Context class object:

public static Toast makeText (Context context, int resId, int duration)

so we are not able to pass an activity or class context means this to Toast.makeText which don't have a subclass of either Context or ContextWrapper class.

getApplicationContext(), getBaseContext(), getApplication(), getParent()

getApplicationContext() Application context is associated with the Application and will always be the same throughout the life cycle.

getBasecontext() should not be used, just use Context instead of it which is associated with the activity and can be destroyed when the activity is destroyed.

makeText method , When to use this and When to use getBaseContext()

What is the deference between Activity and Context ?

As you can see in the Android reference, Activity is an indirect subclass of Context. (Activity extends Context). Essentially what this means is that Activities are 'Contexts', hence using this to get the context in an Activity.

Is the context means the current class ?

Not exactly. Context is literally the context of the application. It pertains to the current state of the application.

From the docs

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.

Why I cant use this Instead of getBaseContext ?

If you are in an Activity, you can use this to reference the current Context, as Activity extends context.

Note that if you are in an inner class, such as an onClickListener, you will have to use YourActivity.this instead of this, to reference the context.

What is the deference between getBaseContext and getApplicationContext ?

Like the name suggests, getApplicationContext() returns the context that references the entire Application, and will be constant throughout any life cycle changes in the app.

getBaseContext() is a bit weirder. You can use it to access a context from inside another context, as detailed in this answer

For your purposes, (in Toast.makeText() ?), using YourActivity.this should work fine.

Difference between getContext() , getApplicationContext() , getBaseContext() and this

  • View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.

  • Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside
    of). Use this instead of the current Activity context if you need a
    context tied to the lifecycle of the entire application, not just the
    current Activity.

  • ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The
    Context referred to from inside that ContextWrapper is accessed via
    getBaseContext().

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

Toast and Intent, both requires reference to context. And getApplication, getApplicationContext, LoginActivity.this and getBaseContext, they all offer reference to the context.

Now the thing confuses is the declaration of different contexts and their specific-usage. To make things simple, you should count two types of context available in the Android framework.

  1. Application Context
  2. Activity Context

Application context is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using Toast, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.

Activity context is attached to the Activity's life-cycle and can be destroyed if the activity's onDestroy() is raised. If you want to launch a new activity, you must need to use activity's context in its Intent so that the new launching activity is connected to the current activity (in terms of activity stack). However, you may use application's context too to launch a new activity but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.

Now referring to your cases:

LoginActivity.this though its referring to your own class which extends Activity class but the base class (Activity) also extends Context class, so it can be used to offer activity context.

getApplication() though its referring to Application object but the Application class extends Context class, so it can be used to offer application context.

getApplicationContext() offers application context.

getBaseContext() offers activity context.

Tips: Whenever you need to manipulate Views then go for
Activity-Context, else Application-Context would be enough.

Why can't I use getBaseContext() here?

Because TCPdumphandler does not extend from Activity. getBaseContext() is a method of that class (technically, of the ContextWrapper class). You need to pass the context to the constructor of TCPdumphandler.

Android Studio cannot resolve method getBaseContext(), but it used to work

Updating Android Studio did the trick. Sometimes, the solution is so simple.

getBaseContext() and getApplicationContext()

Use the following context to solve your problem

calendar1.this

To explain why you are getting that error in the first place. You are using

this.getBaseContext() and this.getApplicationContext() inside the showcalendar, which is a View.OnClickListener class, as you can see from here, it does not have those methods.

getBaseContext() error for parsing data

.My code keeps giving me errors under my getBaseContext() code at the
end and I have no idea why as I think it looks ok!!!

getBaseContext() is a method of ContextWrapper. Since you are getting the cannot resolve the method error, it means that your AsyncTask class is not defined as inner class of a Class that inherits from ContextWrapper (Activity E.g). You could pass it the Context to your AsyncTask .

public class AttemptRegistration extends AsyncTask<String, Integer, String> {
private final Context mContext;
public AttemptRegistration(Context context) {
mContext = context;
}

and then use mContext instead of getBaseContext()



Related Topics



Leave a reply



Submit