Fragment Transactions

jim-wilson-v5

Welcome to Part 2 of this review of the Pluralsight course Improving User Interface Design with Android Fragments by Jim Wilson.

Jim has over 30 years of software engineering experience, with the past 15 years heavily focused on creating mobile device and location-based solutions. He has co-founded multiple software-related startups and has served in a consulting role at several more.

After nearly a decade as a Microsoft Device Application Development MVP, Jim now focuses on developing Android and iOS device applications. Jim’s passion is mentoring software developers. He blogs at hedgehogjim.wordpress.com.

Improving User Interface Design with Android Fragments is the 8th course in the Pluralsight learning path for Android, and this month I am reviewing every course in this learning path.

Also in this series:

Part 1 – Fragment Basics

Fragment Transactions

Dynamically Managing Fragments

Jim says the real power of Fragments comes with being able to rearrange them on the display without leaving the Activity.

We do this using a FragmentTransaction. The process is like any other transaction, the only difference is it is for UI changes.

Jim describes the common steps and methods involved with working with Fragment Transactions:

  1. getFragmentManager();
  2. beginTransaction();
  3. Add/remove/replace
  4. commit();

We can add new fragments to an activity with the add method. Jim shows an example of how we can do this, and their relationship to fragment tags.

We see an example of removing a fragment from an Activity. Jim warns that once a fragment instance is removed it cannot be used within our Activity.

Jim also explains how to replace fragments within a group.

Note: Google have a guide on Performing Fragment Transactions that explains this quite well.

Demo: Dynamically Managing Fragments

We start with an Activity with some fragments in it, and six buttons at the bottom:

  • Add 1
  • Rmv then Add2
  • Replace with 1
  • Move Prev
  • Attach
  • Detach

In the main layout there are several LinearLayouts, one of which has an id of group1.

Then there’s fragment1.xml and fragment2.xml

The code for these fragments contain callbacks for writing to the log files.

The first thing Jim demonstrates is adding a fragment. This involves using the FragmentTransaction add method and passing in group1 as our id.

We commit the transaction at the end of the operation. We see that this successfully adds the fragment to our activity and in logcat we see all the standard events are logged.

Next Jim shows us how to remove a fragment, and again we commit at the end of the operation.

Jim says often when we remove a fragment, we do so to free up space to add another fragment. So what we really want is a replace function, and we see how to do just that.

Managing Fragment State

Jim says Fragments have 3 distinct levels of existence.

  • As a simple Java object
  • Associated with an Activity
  • Rendered UI

We can have the association with the activity as a concept that is distinct from the visibility of the fragment.

In this lesson Jim introduces us to FragmentTransaction.detach and FragmentTransaction.attach

Demo: Managing Fragment State

Jim shows us how to attach and detach fragments, implementing onClickBtnDetach and then onClickBtnAttach.

Then in the emulator we see the fragment added and removed. We see the events logged to logcat and there is a lot less work involved than when we did a full destruction earlier.

So this is much more efficient.

FragmentTransactions and the back button

Jim says FragmentTransactions don’t have any awareness of the back button, and that can be a problem.

When we commit a FragmentTransaction, it often looks like a new screen to the users.

The good news is it isn’t too hard to support this. We just call FragmentTransaction.addToBackStack to create a new entry.

We can optionally name the entry, and that’s useful when we want to move to a specific entry in the back stack.

Demo: FragmentTransactions and the back button

We see this is a really easy fix. We just add one line to each method. For example:

ft.addToBackStack("Add");

Programmatically moving through the back stack

Jim says the simplest way to simulate the back button being pushed is calling popBackStack with no arguments. This is the equivalent of pressing the back button once.

We can also specify the name that we used when it was added to the back stack, to move directly to a specific transaction.

Jim discusses several ways to access the back stack:

  • OnBackStackChangedListener
  • addBackStackChangedListener
  • getBackStackEntryCount
  • getBackStackEntryAt

Demo: Programmatically moving through the back stack

The first thing Jim demonstrates is popBackStack, with an implementation of the FragmentManager.OnBackStackChangedListener interface.

To implement this interface we need to write an implementation of onBackStackChanged. Jim also uses a helper method called LogBackStackEntry.

Logcat shows us how the back stack is changing as we use our app.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s