In Android, usege of ProgressDialog.show() and ProgressDialog.hide() problem during fetching data from the internet
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.
精彩评论