Fragment Object States | Fragment Transactions | Fragment BackStack

In this blog, we are going to discuss about Fragment object states, Fragment transactions and Fragment BackStack behavior.

Let’s first look at the different states of a fragment object.

A fragment exists as :

  • Java object
    When we write,
    Fragment f = new Fragment();
    It exists as a simple java object but at this point FragmentManager is not aware of this object.
  • Associated with activity
    In this step, the fragment object gets associated with the FragmentManager.
    It is at this point when onAttach() method inside the fragment’s lifecycle gets executed where it gets a reference to the activity’s context.
    But at this point, the fragment may or may not be seen by the user. (It is not necessary that every fragment has a UI).
  • Associated with activity + shown to the user as UI
    In this step, the fragment is actually being shown to the user.

Now, what is a Fragment Transaction?

The idea behind the entire fragment transaction concept is that there are several fragments and you wanna manipulate several of them at the same time under single operation.

Steps of a fragment transaction are :

  • Get the FragmentManager
  • Begin a FragmentTransaction
  • Add/Remove/Replace fragments
  • Commit the transaction

Now, let’s talk about each operation in details..

Adding a Fragment 

  • Create the Fragment object
  • Begin the FragmentTransaction
  • Add the Fragment
  • Commit the transaction

Adding a Fragment results in the execution of all the construction lifecycle methods inside the Fragment.

Signature of add method is :
add(id of the layout in which you want to add the fragment, the fragment object, a string tag to get a reference to the fragment later)

Removing a Fragment

  • Get the Fragment object using id or tag
  • Begin the FragmentTransaction
  • Remove the fragment
  • Commit the transaction

Removing a Fragment results in the execution of all the destruction lifecycle methods inside the Fragment.

Signature of remove method is :
remove(the fragment object that you retrieved)

You cannot use the Fragment object after removing it because its UI has been destroyed completely and the Fragment is not associated with the activity anymore.
Though, it may or may not be null at this point because the part of a Fragment existing as a java object is different from the part of it associated with the activity.

Replacing a Fragment

  • Create the Fragment object
  • Begin the FragmentTransaction
  • Add the Fragment
  • Commit the transaction

Replace operation consists of removing one Fragment and adding another Fragment.
So, replacing a Fragment results in the execution of all the destructive lifecycle methods on 1st Fragment and constructive ones on 2nd.

Signature of replace method is :
replace(the layout id where the old fragment is contained, the fragment object)

The layout id is different from the fragment id. Please don’t get confused between these two.
Suppose that your old fragment is contained in a LinearLayout, then you need to pass the id of that LinearLayout as the first parameter.

Fragment BackStack Behavior :

Android has a concept that whenever you navigate from one activity to another activity, the previous activity is not completely destroyed.
It is instead added to a stack from where you can access it when you press the back button.
However, fragments have no sense of the back button by default.

So, whenever you are performing a transaction, call addToBackStack before calling commit on the transaction.

This will result in adding the fragment to a BackStack named as myScreen.
Now, if the user presses the back button, this add operation will be undone and the user is navigated back to the previous state.

In this case, if the user presses the back button, all the three operations performed in this transaction will be undone and the user is navigated back to the previous state.

  • While removing a fragment, if you do not call addToBackStack(), the fragment is destroyed and the user cannot navigate to it.
  • While removing a fragment, if you call addToBackStack(), the fragment is stopped and resumed when the user navigates to it.

This is one of the reasons you might wanna consider for implementing addToBackStack to make your app respond faster to the user.

Leave a Reply