Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Android's FragmentTransaction.commit() Method Thread Safe?

I'm just curious about whether or not I have to call this on the UI-Thread or not. According to the documentation, the commit is run on the UI-thread further on in the future, when it has time to do so. If this is the case, can I run FragmentTransaction.commit() on another thread?

like image 378
cesar Avatar asked Sep 18 '25 18:09

cesar


1 Answers

This question is a year old but I wanted to address it because I suspect it's an issue that a lot of people will encounter.

Working with FragmentTransactions, including calling the commit method, is safe to do on non-UI threads because none of the work that occurs within the FragmentTransaction directly manipulates the UI. The method calls on the FragmentTransaction are simply packaged up as a list of instructions of work to be performed.

Once you call the commit method, that package of instructions is then sent over to the UI thread for processing.

like image 109
Hedgehogjim Avatar answered Sep 21 '25 06:09

Hedgehogjim