How to call Main thread from secondary thread in Android?
-
1Please elaborate on what you are trying to do. – Octavian Helm Dec 08 '10 at 14:15
5 Answers
The simplest way is to call runOnUiThread(...) from your thread
activity.runOnUiThread(new Runnable() {
public void run() {
... do your GUI stuff
}
});

- 12,237
- 1
- 40
- 55
-
4
-
-
1For Swing see http://stackoverflow.com/questions/6567870/what-does-swingutilities-invokelater-do – thoredge Apr 25 '14 at 16:57
-
1This solution is relevant **only** for `Activity` sub-classes ([runOnUiThread](https://developer.android.com/reference/android/app/Activity#runOnUiThread(java.lang.Runnable))) – Eido95 Aug 19 '18 at 13:17
My recommendation to communicate threads in the same process is sending messages between those threads. It is very easy to manage this situation using Handlers:
http://developer.android.com/reference/android/os/Handler.html
Example of use, from Android documentation, to handling expensive work out of the ui thread:
public class MyActivity extends Activity {
[ . . . ]
// Need handler for callbacks to the UI thread
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateResultsInUi();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
[ . . . ]
}
protected void startLongRunningOperation() {
// Fire off a thread to do some work that we shouldn't do directly in the UI thread
Thread t = new Thread() {
public void run() {
mResults = doSomethingExpensive();
mHandler.post(mUpdateResults);
}
};
t.start();
}
private void updateResultsInUi() {
// Back in the UI thread -- update our UI elements based on the data in mResults
[ . . . ]
}
}

- 2,282
- 14
- 18
You'll need a Handler
that passes the information back to the main thread.

- 17,182
- 6
- 67
- 87
Also, it's good to remember that if you get your secondary thread through an AsyncTask, you have the option to call onProgressUpdate(), onPostExecute(), etc., to do work on the main thread.

- 4,668
- 34
- 35
Sample code using HandlerThread
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler responseHandler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
//txtView.setText((String) msg.obj);
Toast.makeText(MainActivity.this,
"Result from UIHandlerThread:"+(int)msg.obj,
Toast.LENGTH_LONG)
.show();
}
};
HandlerThread handlerThread = new HandlerThread("UIHandlerThread"){
public void run(){
/* Add your business logic to pupulate attributes in Message
in place of sending Integer 5 as in example code */
Integer a = 5;
Message msg = new Message();
msg.obj = a;
responseHandler.sendMessage(msg);
System.out.println(a);
}
};
handlerThread.start();
}
}
Explanation:
In above example,
HandlerThread
post aMessage
onHandler
of UI Thread, which has been initialized withLooper
of UI Thread.final Handler responseHandler = new Handler(Looper.getMainLooper())
responseHandler.sendMessage(msg);
sendsMessage
fromHandlerThread
to UI ThreadHandler
.handleMessage
processesMessage
received onMessageQueue
and shows a Toast on UI Thread.

- 37,698
- 11
- 250
- 211