Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do callbacks occur on the main (UI) thread?

Tags:

android

There are a lot of Android SDK APIs where callback handlers are registered. For a concrete example, with MediaPlayer you can set an onCompletionListener callback. Will these callbacks be called from the main (UI) thread? If the answer is "it depends", then I'm looking for some general rules for what callbacks will be called from the main thread versus another thread. The SDK documentation doesn't seem to spell it out. (Maybe I missed it.)

It seems important to know, because if I'm guaranteed main thread callbacks, then I can skip some thread synchronization on data shared between different places in code. If I'm forced to be pessimistic out of ignorance, then I have to write extra synch block code and worry about deadlocks, data integrity, and reduced performance.

like image 367
Erik Hermansen Avatar asked Oct 12 '10 21:10

Erik Hermansen


1 Answers

When in doubt you can use Log.i("TAG", Thread.currentThread().getName()); and see :)

like image 161
dnkoutso Avatar answered Oct 13 '22 02:10

dnkoutso