android - using "this" to refer the "Context" -


i can context methods getapplicationcontext() or getcontext() little confused using "this" "context" in following example:

public class geoactivity extends activity {      private button mtruebutton;     private button mfalsebutton;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_geo);          mfalsebutton = (button) findviewbyid(r.id.false_button);         mfalsebutton.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 // method context                 // context context = getapplicationcontext();                 toast.maketext(geoactivity.this, r.string.incorrect_toast, toast.length_short).show();             }         });     }  } 

in code use geoactivity.this refer context, didn't understand geoactivity.this , pointing to. why in case cannot use this?

an activity context. if use "this", refers object in "this" appears, if use inside method of activity, refers activity. because activity context, can use "this" when need pass context.

the situation complicated fact you're using "this" inside onclicklistener. you're creating anonymous inner class use onclicklistener. that's fine, means if use "this" inside onclicklistener, inside activity, it'll refer onclicklistener rather activity. if you've got nested classes this, can specify instance want refer putting class name in front of "this". in toast.maketext method, "this" refer onclicklistener that's inside activity, "geoactivity.this" refers activity.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -