this
often to reference to current context. But, at some case, why we must use getBaseContext()
instead of this
. (It means when use this
will notice error).
Here is my example:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){
Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show(); //this line
}
At above code, when I change getBaseContext()
to this
will receive error.
Who can explain for me, please.
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 ofContext
orContextWrapper
class*means
getBaseContext()
is method ofContextWrapper
andContextWrapper
is Proxying implementation ofContext
so indirectly we are passing an Context Class Object.or we can also pass 'Activity.this' because
Activity
class is subclass ofContextWrapper
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
toToast.makeText
which don't have a subclass of eitherContext
orContextWrapper
class.OnItemSelected
method this refers to the newOnItemSelectedListener
instance that you used.getBaseContext
is you outer class.getBaseContext() refers to Activity.this
like we want to showing Toast on click of button, we never user this we use Activty.this. So that our Toast display till we are on the same activity. But if we use getApplicationContext() than our Toast will display even we switch the activity.
The getBaseContext() Sometimes confuses new comers to android, instead one could also use the ActivityName which is the current activity in which your are working. so ActivityName.this will replace getBaseContext()
In your example
this
refers to newly createdOnItemSelectedListener
not to any context object. If this code is in activity you can writeYourActivity.this
instead ofgetBaseContext()
.If you use
this
refersOnItemSelectedListener
.And itsInterface not Class
.so it's gives you error ,,, Always Use if you are in ActivityYourActivityName.this