java - AsyncTask do not cancel on cancelling -
i have asynctasks. in oncreate view of dialog fragment creating object of asynctask below (sample code)
@override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { cashinvalidatorlistner = new cashinvalidatorlistner(msessionmanager.getcustomerid(),msessionmanager.getposid(), this); }
now in onclick executing async taks
@override public void onclick(view v) { if(v==ok) { if(mhomeactivity.mprogressdialog!=null && !mhomeactivity.mprogressdialog.isshowing()){ mhomeactivity.mprogressdialog.show(); } cashinvalidatorlistner.execute(); } }
i have added oncancellistner progressbar
@override public void oncancel(dialoginterface dialog) { if(dialog==mprogressdialog) { mdialogextraoptions.cashinvalidatorlistner.cancel(true); toast.maketext(getbasecontext(), "task cancled", toast.length_short).show(); } }
the first time cancel async task cancelled howerver on executing again gives error saying can not execute task executed .
when tried creating object @ onclick each time user click ok button problem solved
@override public void onclick(view v) { if(v==ok) { if(mhomeactivity.mprogressdialog!=null && !mhomeactivity.mprogressdialog.isshowing()){ mhomeactivity.mprogressdialog.show(); } cashinvalidatorlistner = new cashinvalidatorlistner(msessionmanager.getcustomerid(),msessionmanager.getposid(), this); cashinvalidatorlistner.execute(); } }
here works fine ,my question why async task not executing when running second time in first case ?
you may execute asynctask once in instance's lifetime. solved creating new instance of asynctask object , executing newly created object, have done.
(new cashinvalidatorlistner(msessionmanager.getcustomerid(),msessionmanager.getposid(), this)).execute();
Comments
Post a Comment