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?
Asked
Active
Viewed 1,584 times
2 Answers
4
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

Community
- 1
- 1

Rachita Nanda
- 4,509
- 9
- 42
- 66
3
UI thread always have id = 1, so you can try to check:
if(Thread.currentThread().getId() == 1) {
///...
}

x90
- 2,140
- 15
- 25