Anyone here knows how to write a test ( or ideally has an example for this ) to check if a method was called on the UI-Thread?
Answer refered from the following links :
How to check if current thread is not main thread
How to know if this thread is a UI Thread
1) Looper.myLooper() == Looper.getMainLooper()
2) Android app has only one UI thread, so you could somewhere in the Activity callback like onCreate() check and store its ID and later just compare that thread's ID to the stored one.
mMainThreadId = Thread.currentThread().getId();
3) if you want to do something on the UI thread and have any reference to Activity by using
mActivity.runOnUiThread( new Runnable() {
@Override
public void run() {
...
}
});
Hope it helps
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