Progress Dialog Is Not Displaying In Async Task When Asynctask Is Called From Separate Class
Solution 1:
you dont need to start a background method for postExecute. as @baske wrote, you have problem with .get() - that is blocking your thread even if your are using AsyncTask. try someting related to the linked question, so only add your YourActivityClass as a param to the construdtor of JsonData
publicJsonData(YourActivityClass activity)
{
this.activity=activity;
mProgressDialog = newProgressDialog(activity);
}
protectedvoidonPostExecute(String jsondata) {
if (mProgressDialog != null || mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
if(jsondata!=null) {
activity.yourcallback(jsondata)
}
}
And define the yourcallback() in YourActivityClass
privatevoidyourcallback(String data) {
jsonRecordsData=data;
showRecordsFromJson();
}
Solution 2:
user, I think you need to understand better what the reasons are for using an AsyncTask and what the uses are of the callback/hook methods it provides.
Starting with the reason: if you have a long-running task, you cannot run this on the main thread (also called UI thread) because your app will eventually show ANR errors. Now if your long-running task would not need to show output on the screen (when it is done, progress reports, etc) you can very well put it in a worker thread and let it run by itself (possibly even delegating it to a Service to guarantee run-to-completion, but that is another story). However, a lot of times this isn't the case and you want to update your UI based on the outcome/progress of your long-running task. To do this you would have to somehow branch off a thread and do the work there, but, since you can only manipulate the UI from the main thread, you would have to post back the result on the main thread when you are done.
This is where we move to the AsyncTask and its hook methods. An AsyncTask is actually just a Utility class that helps you do exactly what is explained above: put your work on a separate thread and get a callback on your main thread when it is done (and the result is available). Checking the documentation you will find:
onPreExecute(): guaranteed to run on the main thread. Allows you to do stuff (like show a progress dialog) BEFORE the work starts.
doInBackground(): guaranteed to run on a background thread. Do you long running stuff here.
onPostExecute(): guaranteed to run on the main thread AFTER your doInBackground() has finished. The result of your task is now available and you can do stuff with it (like put it on the screen).
Getting back to your suggestions about your .get() method having a problem: since you are calling .execute() on your AsyncTask and .get()-ing the result immediately thereafter, chances are that the background job has not yet finished. Instead you should be doing whatever you wanted to do, starting at the .get() in the AsyncTask's onPostExecute. So if your task downloads an image and you want to show a "downloading" message to the user while it is running, you should do the following:
//pseudo codevoidexampleButtonClicked() {
newAsyncImageDownloader.execute();
}
classAsyncImageDownloaderextendsAsyncTask {
onPreExecute() {
show "downloading";
}
doInBackground() {
downloadImg();
}
onPostExecute() {
hide "downloading";
put downloaded img on ImageView;
}
}
//end of pseudo code
Hope this helps.. Not going to code out your answer, because then you would have learned nothing ;-)
Cheers!
Solution 3:
You can do UI operations only from an UI thread. Try running it on an UI thread.
runOnUiThread(new Runnable() {
publicvoidrun() {
mProgressDialog.show();
}
});
Solution 4:
You can do on override methods onPreExecute() and implement for this code,
@OverrideprotectedvoidonPreExecute() {
super.onPreExecute();
ProgressDialogmProgressDialog= ProgressDialog.show(ActivityName.this, "Wait", "Loading....");
}
and onPostExecute() method implement and dismiss the dialog,
@OverrideprotectedvoidonPostExecute(Void result) {
super.onPostExecute(result);
if (mProgressDialog != null || mProgressDialog.isShowing())
mProgressDialog.dismiss();
}
Solution 5:
Try the following:
@OverrideprotectedvoidonPreExecute()
{
mProgressDialog =ProgressDialog.show(GmailInbox.this, "", "Please Wait",true,false);
super.onPreExecute();
}
Post a Comment for "Progress Dialog Is Not Displaying In Async Task When Asynctask Is Called From Separate Class"