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 FragmentTransaction
s 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.
Use . addToBackStack(null) : If you don't want later pop more than one back stack, but still want to pop one at a time.
To add a fragment to a FragmentManager , call add() on the transaction. This method receives the ID of the container for the fragment, as well as the class name of the fragment you wish to add. The added fragment is moved to the RESUMED state.
Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.
Instead, a FragmentTransaction is used to instantiate a fragment and add it to the activity's layout. While your activity is running, you can make fragment transactions such as adding, removing, or replacing a fragment. In your FragmentActivity, you can get an instance of the FragmentManager, which can be used to create a FragmentTransaction.
Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class. You can group multiple actions into a single transaction—for example, a transaction can add or replace multiple fragments.
As a FragmentTransaction is treated as a single atomic set of operations, calls to both detach and attach on the same fragment instance in the same transaction effectively cancel each other out, thus avoiding the destruction and immediate recreation of the fragment's UI.
A fragment represents a modular portion of the user interface within an activity. A fragment has its own lifecycle, receives its own input events, and you can add or remove fragments while the containing activity is running. This document describes how to create a fragment and include it in an activity.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With