Reputation: 2327
I am new in android and java. I am in a trouble in implementing progress dialog correctly.
I have a code like this
ProgressDialog dialog= new ProgressDialog(Main.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.show();
MY METHOD WHICH GRABS DATA FROM INTERNET;
dialog.dissmiss();
but by implementing this, my method runs well but no progress dialog is visible, again when i comment out the dismiss method, the dialog doesnt stop and i had to force close the app,then how to use this dialog? in aditional dont want to bring any thread here, is there any way to use dialog without any thread? Thanks
Upvotes: 0
Views: 1177
Reputation: 1787
I personally prefer AsyncTask for progress dialog.
In your onPreExecute, create your above code. In doInBackground, do your background tasks and in onPostExecute display an alertdialog saying work done !
Upvotes: 1
Reputation: 4811
Yes, you no need to use Thread class. You can use AsyncTask instead. Start the progress dialog when you call the AsyncTask, dismiss it in the postExecute method.
Upvotes: 1
Reputation: 40193
Problem here is that any long running tasks such as fetching data from the Internet must be run inside a separate thread, otherwise you're likely to get an ANR if your code runs for more then 5 seconds. The ideal solution in my opinion is to implement an AsyncTask
: it lets you run tasks in a separate thread and helps you easily update your UI thread, showing ProgressDialog
s or ProgressBar
s to let your users know that your app is currently busy. Simply place all your ProgressDialog
initialization code inside the onPreExecute()
method of the AsyncTask
, and the dialog.dismiss()
call to onPostExecute()
. Hope this helps.
Upvotes: 4