how to update UI by using progressdialog from several instances of asynctask - android

I have an activity with an inner (asynctask) class.
I'm trying to create a progress dialog ,and in a "for" loop create several instances of my inner (asynctask) class.
those instances supposed to update the message + progress of the progress dialog.
I created the progress dialog inside the activity and showed it inside the
asynctask->onProgressUpdate function.
however the progress dialog is not showing up.
Can anyone tell me if there's another way to update the UI ??

Run AsyncTask successively, one by one, and update your dialog on preExecute or postExecute. And write your for loop in doInBackground, where i is a indicator what task executed.

Related

Add a completion Listener To .setText() Method

I am trying to find a way to check whether a setText method that passes values from the database to the UI has finished and the value is visible on the screen.
How can I accomplish this? The sole purpose for this is for good user experience, I realize it takes afer seconds before the TextView is updated. So I want to add a progress bar and dismiss it when the TextView is updated.
Use AsyncTask .. execute your database query in doInBackground() and
update your UI in onPostExecute()
you can show progress bar in onPreExecute() and dismiss dialog in post
execute

About AsyncTask updating UI thread with dialog

I'm developing an Android app that has a basic structure: activity that requests some action from AsyncTask implementer. The implementer has 3 custom methods that should be able to update UI thread with a Dialog and a postExecute() that should update UI thread with a failuer Dialog if an exception is thrown. Here are some questions:
Where should I create the Dialog object? In the activity class or the AsyncTask implementer? What general guidelines should I follow?
Can I update UI thread with a Dialog without waiting for postExecute()?
How can I update UI thread with a picture? Should I create a custom Dialog or is there an easier way?
If the updates - as dialogs with pictures - come one after another, in a sequence how should I deal with it? Should I create some kind of queue? How would you do it?
Thank you in advance :)
1)There's a couple of different ways you can do this. But personally I usually create the dialog in onPreExecute of the AsyncTask, so that the UI for the task is completely self contained.
2)Yes. You can do it in onProgressUpdate. doInBackground should call publishProgress() which will cause onProgressUpdate to be called on the UI thread.
3)Too few details- where do you want the picture? In an existing image view? On top of the current layout? If you just want to display it in a dialog box, an AlertDialog with custom layout would probably work.
4)Depends on the app. Do you want the user to see all the images, or is it ok to miss images in the middle if a new one is sent?

Progress Dialog is not spinning enough in asynctask in android

I am using progress dialog in my Asynctask class. I put progressdialog.show() on onpreExecte() method of asynctask and dismissing the dialog in onPostExecute. My problem is the wheel in dialog is stops after 2-3 seconds but my background process is working. Can anyone help me to solve this problem? I want to spin the wheel until the background process is over.
Check tutorials how to do asynchronus task in Android:
link
link
link
link
link.
And here are some other StackOverflow questions that are similar:
Progress dialog problem in Android
Progress Dialog on open activity
Progress Dialog while starting new activity
Android: Progress Dialog spinner not spinning
Updating progress dialog
android: showing a progress dialog
Just a guess, but it seems to me that starting the progress dialog INSIDE the AsyncTask causes the dialog to work on the AsyncTask thread, instead of the UI thread.
I suggest moving the progressdialog.show() to just before you call execute() in your Activity.

Handle single progressdialog with parallel multiple asynctask

In my Project I want to parallelly load the data from 3 different url's(webservice) while loading the page.
for this I use 3 different asynctask to load data.
I want to show the progress dialog message as "loading" when first asynctask is started and close dialog after last asynctask is completed(until complete 3 tasks show progress dialog).
Please tell me the way to handle this situation.
There are different ways to handle this situation.
You can use a counter variable initialized with the number of tasks, and this counter gets decremented when an AsyncTask is complete. When the counter is 0, the ProgressDialog is dismissed. You can do this decrement and checking for progress dialog dismissal in each of the AysncTask's onPostExecute.
You may not need the 3 different AsyncTasks. You can use a single AsyncTask with a CountDownLatch for Thread synchronization.
One way you can do is you can create an interface OnThreadFinishListener (with an onThreadFinish() method) and have your activity implement it. All of your AsyncTask should register itself to the listener so each time a thread finishes it calls the onThreadFinish method. in the onThreadFinish method it should check if there is still a thread running in the background (something like if (asyncTask1.getStatus() == AsyncTask.Status.RUNNING || asyncTask2.getStatus() == AsyncTask.Status.RUNNING ... ) )
If all are false then you can dismiss the progress dialog.
You may do it as below:
//define progress dialog object globally in your parent class
ProgressDialog pd = null;
AsyncTask1 onPreExecute:
pd = ProgressDialog.show(this, "title", "loading", true);
AsyncTask2:
//do nothing
AsyncTask3 onPostExecute:
if(pd!=null)
pd.dismiss();

android progressDialog is static

I have an issue with my ProgressDialog object. When I show it first time, it is spinning, but after I dismiss it and show it again, the dialog is static, it it not spinning... Can you please tell me why? All of this happens in the UI thread. Thanks
are you doing any processing in the UI thread during the second show of the ProgressDialog? If so, you need to move the processing to a background thread. Otherwise, the progress dialog does not get the CPU to update(spin) itself.

Categories

Resources