Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android runOnUiThread explanation

Tags:

android

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();  } 
like image 901
user1154644 Avatar asked Jun 29 '12 00:06

user1154644


People also ask

How does runOnUiThread work?

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.

What is runOnUiThread Java?

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() ...

What is the purpose of a UI thread?

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.

How do you use runOnUiThread in non activity class?

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.


1 Answers

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");             }         }     }); } 
like image 146
Cruceo Avatar answered Sep 28 '22 00:09

Cruceo