72290

ProgressDialog appears too late and dissapears too fast

Question:

While I'm updating my database I want to display a progress dialog. My problem is that the ProgressDialog is getting late to appear,after 4-5 seconds, then appears and disappears very fast, it stays on screen few milliseconds almost you can't see it, then new data are shown in the list immediately. This makes me think that the ProgressDialog is waiting for database to be updated(it doesn't take much, about 4,5 seconds) and then it shows on the screen but is dismissing very fast. I would like the ProgressDialog appear immediately I press the 'Update' button and stay on the screen about 4-5 seconds.

class MyAsyncTask extends AsyncTask<Void, Void, Void>{ ProgressDialog myprogsdial; @Override protected void onPreExecute(){ myprogsdial = ProgressDialog.show(MyActivity.this, null, "Upgrade", true); } @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub RefreshDataBase(); } }); return null; } @Override protected void onPostExecute(Void result){ myprogsdial.dismiss(); } }

When I call it, new MyAsyncTask().execute();

Answer1:

Ok I think that this

<blockquote>

runOnUiThread(new Runnable() {

</blockquote>

is causing this behavior.

doInBackground() executes your code in a new thread to the main UI thread. You are then putting the code to execute in this thread back into the main one causing the progress dialog to be delayed at the end and then in postExecute() it gets closed immediately.

A good <a href="http://www.vogella.de/articles/AndroidPerformance/article.html" rel="nofollow">asyntask</a> tutorial can be found here.

Answer2:

You must not use runOnUiThread. What you're basically did is:

<ol><li>Started new non-ui thread</li> <li>From this new non-ui thread you posted a long running task to UI thread. </li> <li>Exited from non-ui thread.</li> <li>Your ui thread now executes long-running operation (RefreshDataBase) and blocks the UI.</li> </ol>

You should call RefreshDataBase() directly. And if this method touches UI, you have to refactor it.

Answer3:

I have solved it, using <a href="https://stackoverflow.com/questions/4538338/progressdialog-in-asynctask" rel="nofollow">this answer of Vladimir Ivanov</a>. I have separated the functionality by the appearance. I have kept the functionality(downloading new data) in doInBackground() and in onPostExecute() I updated the list: get the new adapter,called setListAdaper() and notifyDataSetChanged. Of course, I quit using runOnUiThread(). Thanks to all for hints.

Recommend

  • Updating UI during AsyncTask
  • How to upload audio in soundcloud via my app and i want to send that id also to my server
  • Pressing Button and continuing to next Intent, ProgressDialog
  • Is there Background worker in android? I used AsyncTask suggested in this topic but my progress dial
  • How to prompt user that edits have been made upon changing pages or sorting in Kendo Grid
  • How to distribute an event to all nodes in a (Wildfly) cluster?
  • IE6 changes DOCTYPE to a bad one
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Bypass multiple inheritance in Java
  • GridView breaks while scrolling
  • Android full screen on only one activity?
  • NHibernate Validation Localization with S#arp Architecture
  • Recording logins for password protected directories
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Eraser for UIBezierPath
  • Apache 2.4 - remove | delete | uninstall
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Proper way to use connect-multiparty with express.js?
  • Suggestions to manage Login/Logout transitions
  • Trying to get generic when generic is not available
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?