Activities Vs. Fragments

Activities Vs. Fragments

An exploration of when to use Activities and when to use Fragments


Bryan Herbst

December 11, 2014


  1. 2.

    Activity “An activity is a single, focused thing that the

    user can do.” - Android API documentation
  2. 3.

    Fragment “A Fragment represents a behavior or a portion of

    user interface in an Activity.” “[…] You can think of a fragment as a modular section of an activity.” -Android API documentation
  3. 4.


  4. 5.

    Activity- What it is  Entry point to the application

     Set of Fragments with related purpose  Easy to manage navigation
  5. 7.

    Fragment- What it is  A specific piece of functionality

     Reusable  Relatively lightweight
  6. 8.

    Fragment- What it isn’t  The only option for displaying

    content in an application  Easy to manage navigation
  7. 9.

    Where to start?  Create a navigation map of app

     Focus on hierarchy  Clearly identify sibling screens (e.g. top level nav items) and descendant screens  Determine entry points to the application
  8. 10.

    Choose your weapon  Is this screen a potential entry

    point?  Yes: Activity  No: Maybe Fragment  Is this screen a hierarchical child of another screen (or: should it provide up navigation)?  Yes: Probably Activity  No: Maybe Fragment  Will this UI component be used elsewhere?  Yes: Fragment
  9. 11.

    Case study: Cartwheel  Deep linking  All links route

    through MainActivity  Should have let the framework do the work  Scoreboard  Re-used throughout the app  Event bus for updating
  10. 12.

    The Navigation Drawer Navigation hub vs. replacement for tabs =>

    Activity vs Fragment “Use navigation drawers if:  You don't want to give up the vertical screen real estate for a dedicated tab bar. “ -- Android documentation- App Structure
  11. 13.