progressdialog - Android Progress Dialog Issue -
i've been trying progressdialog work.
at first, had inside activity, not showing no matter how many permutations , combinations tried.
then moved inside asynctask. i'm initializing , showing in onpreexecute, , dismissing in onpostexecute.
@override protected void onpreexecute(){ try { dialog = new progressdialog(context, progressdialog.style_spinner); dialog.settitle("please wait"); dialog.setprogressstyle(progressdialog.style_spinner); dialog.seticon(r.drawable.ic_error); dialog.show(); } catch (exception e) { log.e(task_tag, exceptionutils.getstacktrace(e)); } }
however, i'm not sure should used context. when try activity.getapplicationcontext, following exception thrown android.view.windowmanager$badtokenexception: unable add window -- token null not application.
when tried activity, dialog not shown.
when enclose dialog.show in try / catch, doesn't show. when not enclosed, seems exception takes down app.
i have few questions -
- what recommended way of showing progress dialog? through async task?
- if yes, should used context?
- if 1 of 2 choices mentioned, why app throwing these exceptions?
i recommend using progressdialog inside asynctask, asynctask , activity not linked, it's possible activity dies , asynctask continues execution.
new yourasynctask(context).execute(...);
you have create custom contructor asynctask:
private class yourasynctask extends asynctask<url, integer, long> { private context context; private dialog dialog; public yourasynctask(context context){ this.context = context; } @override protected void onpreexecute(){ try { dialog = new progressdialog(context, progressdialog.style_spinner); dialog.settitle("please wait"); dialog.setprogressstyle(progressdialog.style_spinner); dialog.seticon(r.drawable.ic_error); dialog.show(); } catch (exception e) { log.e(task_tag, exceptionutils.getstacktrace(e)); } } protected long doinbackground(url... urls) { ... } protected void onpostexecute(long result) { if(dialog != null && dialog.isshowing()){ dialog.dismiss(); } .... } }
Comments
Post a Comment