how 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?





Not exactly sure what you mean. You may always have any other thread which sends a message to the UI thread invoking FragmentTransaction.commit().

However, I do not quite understand why you do not call commit immediately after all the transactions.

I would even expect that you may even experience NullPointer Errors should there have been any changed to the fragments or their depending data – during the delay I mean – which cause a different state of your app than when you would call commit immediately.

Also in that context you should call fragmentManager.executePendingTransactions() to ensure that all your changes will be made, otherwise you may assume that the transactions have taken place – which in fact they haven’t yet.


Facebook Comments

Post a comment