1

the AlertDialog.Builder constructor takes a Context as its parameter :

AlertDialog.Builder (Context context),

and i found an example where the parameter is not only this but :

new AlertDialog.Builder(MyClassName.this);

Why that?

Also, i've already seen this thing with Activity, where this time we add .class to the name of the activity we try to reach. Can you please tell me the meaning of those 2 keywords?

Thanks a lot

Paul
  • 6,108
  • 14
  • 72
  • 128
  • Checkout [this](http://stackoverflow.com/questions/5436822/why-does-alertdialog-buildercontext-context-only-accepts-activity-as-a-paramet) one. – Lalit Poptani Dec 14 '11 at 02:46
  • 1
    .class is used for activity/class that you want to have access to and MyClassName.this is actually the context of class MyClassName,which you need inorder to tell the application where you want your view. – Its not blank Dec 14 '11 at 03:02

4 Answers4

1

If you want to refer to the outer class (MyClassName which is a Context in your example) of the inner class where the AlertDialog.Builder is being created you have to use MyClassName.this otherwise this will be referencing the inner class.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
1

just like dtmilano said, AlertDialog is a kind of View , should refer to a context like every other View does.

peanut
  • 1,570
  • 1
  • 12
  • 9
1

The class Activity is a sub-class of Context so you can use it as parameter in your example. Now if for instance, you are inside an onClick method (i.e. button) or inside an inner class or an asynctask, using 'this' would not refer the activity itself so you need to use YourActivity.this.

Instead when you see ClassName.class it usually is because you need to specify wich activity, service or whatever you want to start, in that case the parameter type is Class. For example if you want to start an activity, you use:

Intent intent = new Intent(this or ActivityName.this, AnotherActivityName.class);

For example:

public class MyActivity extends Activity {
....

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        // in this case 'this' refers the current activity instance
        // (but of course you can also use MyActivity.this
        myAdapter = new ArrayAdapter(this, R.layout.list_item, items);

        ...

        myButton.setOnClickListener(new OnClickListener() {
             @Override
                 public void onClick(View arg0) {
                     // here you must use ActivityName.this because
                     // 'this' refers to the OnClickListner instance
                     Intent intent = new Intent(ActivityName.this, AnotherActivityNameActivityName.class);
                     startActivity(intent);
                 }
        });

        ...
}
gwvatieri
  • 5,133
  • 1
  • 29
  • 29
  • thanks gwa, sorry to ask, but if i call an "activity" that i created, this is only a class, isn't it? (not a "service" or anything else, i need to read the doc about this?) Also, if i am "inside" my class, and i call a new Activity through "Intent", `this` or `ActivityName.this` is the same, right? – Paul Dec 14 '11 at 17:29
  • i mean: if i'm inside a callback method, the Context has to be called by my classname + this, and if i'm just inside my class, they are equivalent? so is a "onCreateDialog" method considered as a callback method? Thanks again – Paul Dec 14 '11 at 17:36
  • 1
    No problem paul. Here the thing, whenever you have to pass a context, you need to refer your current activity instance which is done using 'this' (it is a pointer to the current instance). Sometimes happens that you are inside an other object, in that case 'this' refers to that inner object instead of the activity instance. I edited my answer with a small example – gwvatieri Dec 14 '11 at 18:02
  • by the way, do you know why we do the same thing and we add `.class` to call an Activity? if we want to reach `MyActivity` for instance, why do we add `.class` in the intent? Thanks again, this will be my last question! ;-) – Paul Dec 14 '11 at 23:25
  • I guess because underneath the framework runs the right class using reflection, not sure about it, you can take a look to the source code of the Intent class, here the link: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/android/content/Intent.java#Intent.%3Cinit%3E%28android.content.Context%2Cjava.lang.Class%29 – gwvatieri Dec 14 '11 at 23:35
0

this is a java key word to access variable of this class the whole app has a context and which is passed to an activty currenlty on the screen so writting this or classname .this means one and the same thing when you want to write some code in a class which is not an activity and use some of the of actitvty functions you should pass this or class name.this and use the context there in an non activity class