Android: why must use getBaseContext() instead of this
Solution 1
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.
Solution 2
In your example this
refers to newly created OnItemSelectedListener
not to any context object. If this code is in activity you can write YourActivity.this
instead of getBaseContext()
.
OnItemSelectedListener listener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){
// this.equals(listener) == true;
// getBaseContext() here means YourActivity.this.getBaseContext()
// getBaseContext() called from outer context object (activity, application, service)
}
}
Solution 3
If this code is in the Activity MyActivity
, you could also replace getBaseContext()
by MyActivity.this
.
This is because this
refers to the OnItemSelectedListener instance, not to the Activity.
getBaseContext()
refers to the Activity context.
Solution 4
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.
Solution 5
OnItemSelected
method this refers to the new OnItemSelectedListener
instance that you used.
getBaseContext
is you outer class.
hqt
Those who don't speak math are doomed to speak nonsense. My brief profile: https://github.com/hqt/profile
Updated on November 26, 2020Comments
-
hqt over 3 years
this
often to reference to current context. But, at some case, why we must usegetBaseContext()
instead ofthis
. (It means when usethis
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()
tothis
will receive error.Who can explain for me, please.
-
hqt about 12 yearsDoes it true when say
each thread has it own context object
? So,onItemSelectedListener
is on different thread of UIThread, so we cannot usethis
keyword, right ? -
hqt about 12 yearssame question as I have asked Sergey, Does it true when say each thread has it own context object ? So, onItemSelectedListener is on different thread of UIThread, so we cannot use this keyword, right ? Thanks :)
-
louiscoquio about 12 yearsAFAIK
this
refer to the instance where you are. If you're in an inner (or anonymous) class, it refers to that inner class. No matter if you're in the thread 1 or 30. -
Sergey Glotov about 12 years1. AFAIK,
onItemSelectedListener
is on UI thread. 2. It is not related to the threads.this
is a reference to the current object. InonItemSelected()
method current object isOnItemSelectedListener
. -
Raz about 12 yearslouiscoquio is right. The thread doesn't have any relation to the context. "this" refers to the instance.
-
NineToeNerd almost 8 yearsI thought "this" refers to OnItemSelectedListener - not Spinner. As in answer by Sergey Glotov below.