android - Check if asynctask with Progress Dialog is finished in non activity class -
i have created non activity java class doing same calculation different activities. class has asynctask progress dialog in it. in cases calculation not last operation of activity , goes fine, when progress dialog goes lost.
example:
myjavaclass docalculations= new myjavaclass (someactivity.this); docalculations.do(); //<------ method has asysctask progress dialog finish(); result:
java.lang.illegalargumentexception: view=com.android.internal.policy.impl.phonewindow$decorview{2bbf820e v.e..... r......d 0,0-1026,483} not attached window manager how can wait asynctask finish , finish activity?
additional question: using asynctask in non activity class bad practice ?
if want keep activity active until asynctask has finished job, can define callback method in activity gets called when task has finished , can react appropriately:
in activity:
private boolean finishaftercurrenttask = false; public void ontaskfinished() { if (finishaftercurrenttask) { // set true when running last task finish(); } } in asynctask:
protected void onpostexecute(object result) { progressdialog.dismiss(); activity.ontaskfinished(); } i'm assuming you're keeping reference activity in myjavaclass. in case you're calling asynctask non-ui thread, should run callback (and methods involving dialog) via activity.runonuithread().
as second question - if use asynctask in class methods can called thread other ui thread, it's necessary use runonuithread() perform operations on ui, such changing contents of view or showing/hiding dialog, beause onpreexecute() , onpostexecute() called same thread execute().
on other hand, if start task inside 1 of ui callbacks, guaranteed run on ui thread, don't need worry that.
Comments
Post a Comment