An activity is the starting point for a screen, but if we put all the views and their logic together it quickly dissolves into a huge mess! A much better way to construct your views is with fragments to further encapsulate the view logic into self-contained pieces.
It breaks the logic of the activity up into self-contained pieces that handle user input. This is great for a lot of reasons. It lets you reuse these components in other layouts to support different form factors without changing code, just constructing a new layout with a different arrangement of fragments and views.
Activities hold fragments, which can't live outside a host. The activity reacts to notifications from the fragment that the view should be altered. The fragment holds a reference to the hosting activity and can access it by calling
Consider the example application above. The tablet version has a list of articles on the left side of the screen and a detail pane on the right. The phones uses two separate activities to show these views.
When a user selects a new article to view, the fragment on the right should update. You wouldn't want the article detail, such as an article about Doctor Who, to be responsible for updating the view to show an article about cats.
It also doesn't make much sense for the list to know a lot of information about the detail it's going to show. Here you would use the Activity to determine which detail view next. This becomes important when you also have a phone implementation that would appear in totally different activities.
So, how do we do it? When the user taps an article in the list, an event is triggered and the Activity responds to this received event. In the case of the tablet, it will simply update the right pane. In the case of the phone, it would start a new activity.
Fragments weren't always around. They introduced a few years ago with the first tablets and Honeycomb (Android 3.x).
Dashboard, July 2014: https://developer.android.com/about/dashboards/index.html
It makes it complicated to support the full range of OS versions, but it's becoming more and more compelling to sunset the older versions as their percentage continues to drop. As of the writing of this book, the percentage of OSes in the wild that don't include
Fragment's is only 14% (combined percentages of Froyo and Gingerbread).
However, if you do intend to support older versions, they have back ported it all the way back to 1.6 using the support library.