Search code examples
androidandroid-fragmentsfragmenttransaction

When should setReorderingAllowed() be called on a FragmentTransaction?


At this part of a talk given at Google I/O 2017, the speaker introduces a new API for setReorderingAllowed() than can be called on a FragmentTransaction.

The speaker explains:

It allows all the execution to happen all at once without changing your fragment state and then at the very end we bring up all the fragments that need to be brought up and tear down all the fragments that need to be torn down…so we can optimize this for you.

And shows the following code sample:

fragmentManager.beginTransaction()
    .replace(R.id.container, fragment1)
    .addToBackStack("state1")
    .setReorderingAllowed(true)
    .commit();

fragmentManager.beginTransaction()
    .replace(R.id.container, fragment2)
    .addToBackStack("state2")
    .setReorderingAllowed(true)
    .commit();

Wouldn't committing the FragmentTransactions separately negate any optimization that .setReorderingAllowed(true) gives you because they are occurring separately?

Since this is a newly announced API it looks like there is no documentation currently available.


Solution

  • I believe the method they mention already exists since support library 25.1.0 but is currently called setAllowOptimization(true). The documentation clearly states "optimizing operations within and across transactions" so it will optimize distinct transactions.