I used HandlerThread and then used its looper to create a new Handler so that it can run operations on a non-UI thread. In the runnable which is posted to the handler, I added Toast messages to be displayed. I expected that to cause a problem as you can't touch UI components from the non-UI thread, but it still works and that toast is still being shown. Can anyone explain why toast is being displayed from the non-UI thread?
//Inside a Fragment class
private Handler handler;
private HandlerThread mHandlerThread = null;
public void startHandlerThread() {
mHandlerThread = new HandlerThread("HandlerThread");
mHandlerThread.start();
handler = new Handler(mHandlerThread.getLooper());
}
private Runnable submitRunnable = new Runnable() {
@Override
public void run() {
//do some long running operations here
//Thread.sleep(2000);
//Check whether currentLooper is the Main thread looper
boolean isUiThread = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
? Looper.getMainLooper().isCurrentThread()
: Thread.currentThread() == Looper.getMainLooper().getThread();
if (isUiThread) {
// You are on the UI thread
Log.d("Thread", "Main thread");
} else {
// You are on the non-UI thread
Log.d("Thread", "Not Main thread"); //This will be printed
}
Toast.makeText(getContext(), "toast is shown", Toast.LENGTH_SHORT).show();
}
};
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handler.post(submitRunnable);
}
});
I checked Toast.java and saw that the looper initializes itself with Looper.myLooper().
if (looper == null) {
// Use Looper.myLooper() if looper is not specified.
looper = Looper.myLooper();
}
From the doc:
myLooper(): Return the Looper object associated with the current thread.
And the currentThread is the HandlerThread, not the main thread. Hence, I am unable to understand how the toast is being displayed from the non-UI thread, or if it is something plain simple I am missing to see.