开发者

In Android, usege of ProgressDialog.show() and ProgressDialog.hide() problem during fetching data from the internet

开发者 https://www.devze.com 2023-02-25 00:50 出处:网络
When I define progress dialog functions such as public static void showLoadingBar(Context context) { dialog=new ProgressDialog(context);

When I define progress dialog functions such as

    public static void showLoadingBar(Context context)
{
    dialog=new ProgressDialog(context);
    dialog.setMessage("Please wait");
    dialog.show();
}
public static void hideLoadingBar()
{
    dia开发者_如何学编程log.dismiss();

}

I wanna use it like following:

    UiManager.getInstance().showLoadingBar(this);
    FetchData();
    UiManager.getInstance().hideLoadingBar();

But I have never be able to show LoadingBar until I comment the UiManager.getInstance().hideLoadingBar(); line such like that

    UiManager.getInstance().showLoadingBar(this);
    FetchData();
    //UiManager.getInstance().hideLoadingBar();

What this cause is, always ProgressBar on the screen. Is there anyway to get rid of this issue?


FetchData() seems to be an asynchronous operation. So, before the actual operation is complete, the function returns and hides the loading bar. I suggest you use an AsyncTask.

To show a progress dialog while an AsyncTask runs, you may call show() in onPreExecute() and call hide() in onPostExecute(). Call FetchData() from doInBackground(). This will start the ProgressDialog before the AsyncTask does it's background method and will stop the ProgressDialog when it completes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号