For the More Curious: Why Use Fragment Arguments?

In this chapter you added a newInstance(…) function to your fragment to pass along arguments when creating a new instance of fragment. While this pattern is helpful from a code organization standpoint, it is also necessary in the case of fragment arguments. You cannot use a constructor to pass arguments to a fragment instance.

For instance, you might consider adding a one-argument constructor to CrimeFragment that expects a UUID crime ID as input, rather than adding a newInstance(UUID) function. However, this approach is flawed. When a configuration change occurs, the current activity’s fragment manager automatically re-creates the fragment that was hosted before the configuration change ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.