android - ProgressDialog not executing the first time -
i've got activity implements retained fragment perform time consuming task. allows me rotate screen without loosing reference async task , without stopping being executed. have button on activity launch async task. everytime activity created, example due rotated screen, activity checks if retained fragment has async task running. if so, shows progressdialog let user know there still tasks running on background. attach code mentioned. works. however, strange works everytime first! when push button launch async process first time, progressdialog not shown, eventhough "onpreexecute()" called , line "progressdialog.show()" executed. if rotate screen after push button progressdialog shows , when process ends, if push button again works fine. said, working time except first one. idea why?
thanks!
activity
public class activity extends appcompatactivity implements activitytaskfragment.taskcallbacks{ private progressdialog progressdialog; private static final string tag_task_fragment = "task_fragment"; private activitytaskfragment activitytaskfragment; //some additional code here @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_crear_turno); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); //------------------------------------------------------ setlisteners(); setfragment(); createprogressdialog(); showprogressdialog(); } private void showprogressdialog() { if(activitytaskfragment!=null) { if(activitytaskfragment.isrunning()) { progressdialog.show(); } } } private void setfragment() { fragmentmanager fm = getfragmentmanager(); activitytaskfragment = (activitytaskfragment) fm.findfragmentbytag(tag_task_fragment); // if fragment non-null, being // retained across configuration change. if (activitytaskfragment == null) { activitytaskfragment = new activitytaskfragment(); fm.begintransaction().add(activitytaskfragment, tag_task_fragment).commit(); } } private void setlisteners() { button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { activitytaskfragment.execute(); } }); } //fragment interface implementation @override public void onpreexecute() { if(progressdialog!=null) progressdialog.show(); } @override public void oncancelled() { if(progressdialog!=null) { if(progressdialog.isshowing()) { progressdialog.dismiss(); } } } @override public void onpostexecute() { if(progressdialog!=null) { if(progressdialog.isshowing()) { progressdialog.dismiss(); } } } private void createprogressdialog() { if(progressdialog == null) { progressdialog = new progressdialog(activity.this); progressdialog.settitle("executing job"); progressdialog.setmessage("please wait..."); } } }
taskfragment
/** * fragment manages single background task , retains * across configuration changes. */ public class activitytaskfragment extends fragment { /** * callback interface through fragment report * task's progress , results activity. */ interface taskcallbacks { void onpreexecute(); void oncancelled(); void onpostexecute(); } private taskcallbacks mcallbacks; private checktask mtask; @override public void onattach(activity activity) { super.onattach(activity); mcallbacks = (taskcallbacks) activity; } /** * hold reference parent activity can report * task's current progress , results. android framework * pass reference newly created activity after * each configuration change. */ public void execute() { mtask.cancel(true); mtask = new checktask(); mtask.execute(); } public void cancel() { if(mtask != null) { mtask.cancel(true); } } @override public void onattach(context context) { super.onattach(context); if(context instanceof activity) { activity activity = (activity) context; mcallbacks = (taskcallbacks) activity; } } /** * method called once when retained * fragment first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // retain fragment across configuration changes. setretaininstance(true); mtask = new checktask(); } /** * set callback null don't accidentally leak * activity instance. */ @override public void ondetach() { super.ondetach(); mcallbacks = null; } /** * dummy task performs (dumb) background work , * proxies progress updates , results activity. * * note need check if callbacks null in each * method in case invoked after activity's , * fragment's ondestroy() method have been called. */ private class checktask extends asynctask<void, void, void> { @override protected void onpreexecute() { if (mcallbacks != null) { mcallbacks.onpreexecute(); } } /** * note not call callback object's methods * directly background thread, result * in race condition. */ @override protected void doinbackground(void... ignore) { /* long time consuming task */ return null; } @override protected void oncancelled() { running = false; if (mcallbacks != null) { mcallbacks.oncancelled(); } } @override protected void onpostexecute(void ignore) { running = false; if (mcallbacks != null) { mcallbacks.onpostexecute(); } } } public boolean isrunning() { if(mtask!=null) { if(mtask.getstatus() == asynctask.status.running) { return true; } else { return false; } } else { return false; } } }
Comments
Post a Comment