android - How to prevent double-click on refresh button -
i'm developing android app i've refresh button in action bar. button call function re-open same activity. activity contains asynctask load content.
at moment i'm encountering problem. when click on refresh button works fine, if click on refresh button when asynctask still working (i've progress bar check status) app crashes.
the error receive is: nullpointerexception
is possible disable button until activity (and asynctask) loaded?
in button's onclicklistener
, execute asynctask
, add code:
button.setenabled(false);
in onpostexecute()
method of asynctask
, place this:
button.setenabled(true);
if give 'cancel' option user(i.e. if have overridden oncancelled()
method in asynctask
), enable button in oncancelled()
.
edit 1:
declare boolean
flag in activity:
boolean menubuttonisenabled = true;
in onclicklistener
, set flag false:
menubuttonisenabled = false;
in onpostexecute()
method of asynctask
:
menubuttonisenabled = true;
override onprepareoptionsmenu(menu)
method in activity:
@override public boolean onprepareoptionsmenu (menu menu){ super.onprepareoptionsmenu(menu); menuitem button = menu.finditem(r.id.whatever_menu_button); if(menubuttonisenabled){ button.setenabled(true); } else { button.setenabled(false); } return true; }
Comments
Post a Comment