android - Is it OK to use "mContext" (initialized at onCreate)? -


is bad habit, , why if is? in every activity adding right after oncreate...

mcontext = this; 

and use in other cases context required? example

toast.maketext(mcontext, mcontext.getstring(r.string.somestring), toast.length_long); 

edit: if have this...how context should passed? because this cannot applied (because of view.onclicklistener()).

somebutton = (button) findviewbyid(r.id.somebutton); somebutton.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view v) {         toast.maketext(mcontext, mcontext.getstring(r.string.warning), toast.length_long).show();     } }); 

is bad habit, , why if is?

yes, bad habit. waste of code. this shorter mcontext, , have line of code setting data member.

i disagree mr. damiean's suggestion of using getapplication(). use getapplication() when need application object. neither need nor want application object creating toast -- activity suitable context use there. application object fails work in many places, particularly when dealing things involving ui.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -