3

In Android using Java, when using a listener callback method that can be called from multiple threads according to the documentation, is there any harm in forcing it to run on the main thread by surrounding the method body with runOnUiThread like this:

@Override
public void onSomeBackgroundTaskCameToAnEndDueToReasonNumber21(final int taskID) {

    runOnUiThread(new Runnable() {
        // put all code here 
    }
}

in order to prevent accidentally "touching views" from a background thread in subsequent chains of method calls?

When the Android developers choose to make one of these callback fire on a background thread, what is the reason?

An example of such a class is UtteranceProgressListener used on TextToSpeech objects.

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
  • 1
    Good question but interesting to see that no answer yet. I think if you have to "touch view" then it should be safely call (runOnUi or whatever preferred) otherwise it should not be a concern. The reason I can think of is that the TTS engine is a global, more than one process could be access it or even same process could be calling speak simultaneously so going for background thread is a optimal solution I believe. – eC Droid Jan 09 '20 at 06:45

0 Answers0