Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the cases when I should NOT use FragmentManager's executePendingTransactions() (within the main thread)?

I'm learning how to use fragments, and there are lots of little gotchas that I've come across so far. The executePendingTransactions method of FragmentManager looks like it might have a few of its own, specifically because of this note in its description in the official Android docs:

If you want to immediately executing any such pending operations, you can call this function (only from the main thread) to do so. Note that all callbacks and other related behavior will be done from within this call, so be careful about where this is called from. http://developer.android.com/reference/android/app/FragmentManager.html#executePendingTransactions()

Sounds to me like a pretty cryptic warning. How would these "callbacks and other related behavior" affect my application? Can this cause problems when calling this function from the main thread? I could not find any specific examples, so I'm asking for help from anyone who has demystified this warning: what are some examples of when I should not use executePendingTransactions in the main thread, and what horrible things would happen if I did? Or is it always safe to use as long as it's in the main thread?

like image 471
LearningNerd Avatar asked Apr 27 '13 14:04

LearningNerd


2 Answers

You should NOT call this method when you are in a thread other than UI thread.

It is specified in the same lines:

If you want to immediately executing any such pending operations, you can call this function (only from the main thread) to do so.

like image 135
Sagar Waghmare Avatar answered Sep 19 '22 20:09

Sagar Waghmare


you can not call it from inside an already executed transaction. you will get "Recursive entry to executePendingTransactions"

for an example: you replace a fragment, and then call executePendingTranstactions. and in the onCreate of that fragment you replace another fragment (with the activity's fragmentManager)

like image 25
ndori Avatar answered Sep 19 '22 20:09

ndori