I am trying to display a progress dialog during the onCreate() method of one of my activities, have the work done to populate the screen done in a thread, and then dismiss the progress dialog.
Here is my onCreateMethod()
dialog = new ProgressDialog(HeadlineBoard.this); dialog.setMessage("Populating Headlines....."); dialog.show(); populateTable();
The populateTable method contains my thread and the code to dismiss the dialog, but for some reason. The activity comes up blank for about 10 secs(doing the populateTable() work), and then I see the screen. I never see the dialog displayed, any ideas?
Here is the populateTable() code:
//Adds a row to the table for each headline passed in private void populateTable() { new Thread() { @Override public void run() { //If there are stories, add them to the table for (Parcelable currentHeadline : allHeadlines) { addHeadlineToTable(currentHeadline); } try { // code runs in a thread runOnUiThread(new Runnable() { @Override public void run() { dialog.dismiss(); } }); } catch (final Exception ex) { Log.i("---","Exception in thread"); } } }.start(); }
Basically what runOnUiThread() will do is - Runs the specified action on the UI thread. It will check the current thread and if it finds its the MainThread it will execute that task immediately , otherwise first it will switch you to app MainThread and then it will execute the given task.
The Java Runnable is an Interface that has a single run method that is the function that is executed on the UI thread – this means it is a SAM (Single Abstract Method) and we can use a lambda to simplify the code, but first let's use another anonymous class: runOnUiThread( new Runnable() { @Override. public void run() ...
User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.
You need your Activity object to do this. Pass your Activity's this reference through the constructor and use it in your AsyncTask. Since runOnUiThread is a public method in Activity class, you cannot use it in some other Custom class or class that extends other than Activity itself.
If you already have the data "for (Parcelable currentHeadline : allHeadlines)," then why are you doing that in a separate thread?
You should poll the data in a separate thread, and when it's finished gathering it, then call your populateTables method on the UI thread:
private void populateTable() { runOnUiThread(new Runnable(){ public void run() { //If there are stories, add them to the table for (Parcelable currentHeadline : allHeadlines) { addHeadlineToTable(currentHeadline); } try { dialog.dismiss(); } catch (final Exception ex) { Log.i("---","Exception in thread"); } } }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With