how do I post messages to the main UI looper from a thread?
I’m working on a stand-alone helper class, which should display a window.
A bad solution would be to pass a View via the construct, and then use View.post(),
but I’m looking for a better solution.
I thought this would work, but I get the common “no on ui thread” exception.
public class Example {
private Context context;
public Example(Context context) {
this.context = context;
}
public void build() {
}
public void show() {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "test", Toast.LENGTH_LONG);
}
});
}
}
Simply pass the context and make use of the runonUiThread method, like this,