Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: why must use getBaseContext() instead of this

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.

like image 799
hqt Avatar asked Mar 07 '12 16:03

hqt


People also ask

What is the use of getBaseContext () in android?

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

What is the difference between getContext () getApplicationContext () getBaseContext () and this?

getApplicationContext() - Returns the context for all activities running in application. getBaseContext() - If you want to access Context from another context within application you can access. getContext() - Returns the context view only current running activity.

What is the difference between this context and getApplicationContext which one to use when?

This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component. Actually in general it is better to use getApplicationContext() since it will less likey lead to memory leaks.

What is the difference between base context and Applicationcontext?

getApplicationContext() points to your application instance which is Non-UI and long living context. baseContext is the base of your Activity Context which you can set using a delegate pattern. You already know you can create Context with any xyz configuration you want.


2 Answers

  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.

like image 144
ρяσѕρєя K Avatar answered Sep 29 '22 19:09

ρяσѕρєя K


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)     } } 
like image 22
Sergey Glotov Avatar answered Sep 29 '22 19:09

Sergey Glotov