Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 517
Dick Lucas Avatar asked May 24 '17 18:05

Dick Lucas


People also ask

What does addToBackStack null do?

Use . addToBackStack(null) : If you don't want later pop more than one back stack, but still want to pop one at a time.

What function is called to start a fragment transaction?

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.

What is the use of addToBackStack in Android?

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.

What is the use of fragmenttransaction in Android?

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.

What is a transaction in fragment?

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.

How is a fragmenttransaction treated as an atom?

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.

What is a fragment in an activity?

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.


Video Answer


1 Answers

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.

like image 80
BladeCoder Avatar answered Nov 09 '22 10:11

BladeCoder