Slide 1

Slide 1 text

Activities vs. Fragments Bryan Herbst – Senior Software Engineer, The Nerdery

Slide 2

Slide 2 text

Activity “An activity is a single, focused thing that the user can do.” - Android API documentation

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

IO

Slide 5

Slide 5 text

Activity- What it is  Entry point to the application  Set of Fragments with related purpose  Easy to manage navigation

Slide 6

Slide 6 text

Activity- What it isn’t  Scary or evil  Super lightweight  Reusable

Slide 7

Slide 7 text

Fragment- What it is  A specific piece of functionality  Reusable  Relatively lightweight

Slide 8

Slide 8 text

Fragment- What it isn’t  The only option for displaying content in an application  Easy to manage navigation

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Multi-fragment layouts  AKA “tablet layouts”  Doesn’t play nice with the single activity navigation drawer!