Android: why must use getBaseContext() instead of this
getApplicationContext ()
returns the application context of the entire application life cycle,when application will destroy then it will destroy also.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 theSpinner
instance because we are using this withinonItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3)
method which is fromSpinner
class andSpinner
inherit this method fromAdapterView.OnItemSelectedListener
interfacegetBaseContext()
is the method ofContextWrapper
. AndContextWrapper
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.
- Application Context
- 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
How to Animate a Slide in Notification View That Pushes the Content View Down
Sdcard Content Exist But Cant See Them
Elevation on Android Lollipop Not Working
Android - Detecting Application Launch from Home or History
How to Setup Appium in Ubuntu for Android
Android: How to Change the Datepicker View Date Format from Mm/Dd/Yyyy to Dd/Mm/Yyyy
How to Restrict Content Provider Data Across Applications
How to Implement a Gcm Hello World for Android Using Android Studio
Realm with Pre Populated Data into Assets
Gradle 0.9.+/0.9.2 Error: Unable to Load Class 'Com.Android.Builder.Testing.Api.Deviceprovider'
Single Click and Double Click of a Button in Android
How to Change Images on Imageview After Some Interval
Notifydatasetchanged Not Working on Recyclerview
Android SQLite Selection Args[] with Int Values
How to Configure Launcher Activity Programmatically in Android
How to Get National Holidays of Selected Country