Fighting Fragmentation with Fragments
     Optimizing Android apps for Honeycomb and beyond

                      Andreas Grunewald
                           Nov 2011
                      Barcamp Saskatoon
Andreas Grunewald
               http://grunewaldsrobots.com


goo.gl/b3bu6
iOS Development is
so much more awesome than
   Android Developmemt
Android is way too   Fragmented
Android is a mess, say developers
                            - Fortune
Android Fragmentation: a
            real problem, developers say
                             - Ubergizmo




Developers: Android Fragmentation
is a “Huge” Problem
                        -Slashgear
What problems do Developers see?




5. Ability to get paid. iOS leads here too, followed by BlackBerry.




                   -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
What problems do Developers see?



4. App visibility. "iOS continues to lead," Baird reports, "followed by
   Blackberry, with Android still receiving poor marks in this category."
   Developers are particularly concerned about the level of "junk" apps
   in the Android ecosystem.




                   -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
What problems do Developers see?




3. Ease of development. iOS outscored Android, but both were
   considered far easier to develop for than, say, Research in Motion's
   (RIMM) BlackBerry OS or Nokia (NOK) Symbian.




                   -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
What problems do Developers see?



2. Store fragmentation. Several developers expressed concern over
   Android app store fragmentation. "Generally," Baird reports,
   "developers seem to prefer a unified, single store experience like
   Apple's App Store."




                   -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
What problems do Developers see?



1. Device fragmentation. 56% of Android developers said that
   operating system fragmentation among the various Android
   devices was a meaningful or "huge" problem, a percentage that
   actually increased over the past three months.




                  -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
Android Orphans:
Visualizing a Sad History of Support




                                                                                -theunderstatement
 http://theunderstatement.com/post/11982112928/android-orphans-visualizing-a-sad-history-of-support
Current
iOS Devices
Current
Android Devices
Are we
ARE WE            ED ?
© http://www.americanprogress.org/cartoons/2008/08/081408.html
© http://emilyandroid.deviantart.com/art/Yay-we-re-doomed-173226907
Let's have a look... shall we?
Platform Dashboard (Nov 3rd)
                  2.2 & 2.3 together 85.1% of the market
                  (With 2.1 included even 95.8%)
                  Honeycomb only 1.9%

                  Platform          Codename      API Level       Distribution
                  Android 1.5       Cupcake                   3   0.9%
                  Android 1.6       Donut                     4   1.4%
                  Android 2.1       Eclair                    7   10.7%
                  Android 2.2       Froyo                     8   40.7%
                  Android 2.3 -     Gingerbread               9   0.5%
                  Android 2.3.2
                  Android 2.3.3 -                         10      43.9%
                  Android 2.3.7
                  Android 3.0       Honeycomb             11      0.1%
                  Android 3.1                             12      0.9%
                  Android 3.2                             13      0.9%
4.0 | 19.10
                   2011




                          3.2 | 15.07
                          3.1 | 10.05
                          3.0 | 22.02
Android Versions




                          2.3 | 06.12
                   2010




                          2.2 | 20.05
                          2.1 | 12.01
                          2.0 | 26.10
                          1.6 | 15.09
                   2009



                          1.5 | 30.04
                          1.1 | 09.02
                          1.0 | 23.09
                   2008
Screen Sizes / Densities (Nov 3rd)



                      89.5% Normal Screen Size
                      73.8% hdpi + 23.7% mdpi = 97.5%
So how do you do it ?
Fragments




Introduced in Android 3.0 Honeycomb. What about pre 3.0 Devices ?
The Support Package
aka Compatibility Library
What's in it ?
●   Fragment
●   FragmentManager
●


●
    FragmentTransaction
    ListFragment
                            UI
●   DialogFragment
●   LoaderManager
    Loader
                          Data
●


●   AsyncTaskLoader
●   CursorLoader
Action Bar




Android 3.0 and up only, not in Support Package for pre 3.0 devices
Fo
Help is on the way...




                                kmr
                                      eo
                                        nG
                                        i tH
                                            ub
          http://actionbarsherlock.com
Let's look at Fragments again
How do you structure an App
 (The pre Honeycomb way)
How do you structure an App
 (Honeycomb and beyond)
That doesn't look much different!?




               vs
Let's turn our phone sideways
How about Tablets ?
And of course Tablet landscape
So how many activites, fragments and layouts
                are there ?
Put it all together
So much for the theory....
Download the Demo App from the Market




                http://goo.gl/y9C6c
Patterns when using Fragments

Event Listener Interface
   ●   communication between Fragment and Activity
   ●   separation of concerns / the fragments don't know about the
       activities -> easier to reuse across Activities
Using FrameLayouts as placeholders and adding fragments
dynamically
   ●   allows hiding of the viewgroup
   ●   Fragments can be given arguments when they are needed
   ●   easier to refactor if no fragments are in layout.xml
Event Listener Interface
class MyActivity implements OnTitleListener



class TitlesFragment extends ListFragment {

 public interface OnTitleListener {
   public void onTitleSelected(int titleId);
 }

 public void addOnTitleListener(OnTitleListener onTitleListener) {
   listener = onTitleListener;
 }

  public void onListItemClick(ListView l, View v, int position, long id) {
    ...
    listener.onTitleSelected(position);
  }
...
FrameLayout
Only use for static initialization and Fragments that are always present
<fragment android:layout_height="match_parent" android:layout_weight="1"
android:id="@+id/titles" class="demo.gwr.fightfrag.fragments.TitlesFragment"
android:layout_width="0px"></fragment>


Use the framelayout for dynamic fragments, by hiding the Layout you can make the fragments
appear when they are needed
<FrameLayout android:id="@+id/chapters" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent"
android:visibility="gone" />
FrameLayout
FragmentManager fragMgr = getSupportFragmentManager();

ChaptersFragment chapters = (ChaptersFragment)
fragMgr.findFragmentById(R.id.chapters);

if (chapters == null){
    chapters = ChaptersFragment.newInstance(titleId);
    chapters.addOnChapterListener(this);
}

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.chapters, chapters);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();

View chaptersFrame = findViewById(R.id.chapters);
chaptersFrame.setVisibility(View.VISIBLE);
Resources
●   Android Developer Portal – SDK, Sample Code, Reference Docs
    http://developer.android.com
●   Commonsware – Android Ebooks, Free Sample Code
    http://commonsware.com/
●   StackOverflow – Questions and Answers
    http://stackoverflow.com/questions/tagged/android
●   Android IRC Channel
    #android-dev on freenode
People
Alexander Lucas                                      Kirill Grouchnikov
DevAdv might answer you on Stackoverflow             Android User Interface Engineer
Andy Rubin                                           Matias Duarte
The father of Android                                The creative mind behind Androids UI, Designed WebOS
Chet Haase                                           Reto Meier
Android Graphics Engineer                            Author Professional Android Development
Dan Morrill                                          Romain Guy
Android Engineer                                     Android Graphics Engineer, Androdi Wallpapers
Dianne Hackborn                                      Roman Nurik
Android Framework Engineer                           Android Developer Advocate
Ed Burnette                                          Tim Bray
Author Hello Android                                 Android Developer Advocate from Vancouver
Jake Wharton                                         Tor Norbye
Developer of Action Bar Sherlock                     Works on the ADT & Eclipse Plugin
Jean Baptiste Queru                                  Xavier Ducrohet
Android Open Source Project                          Android Developer Tools Lead

                                   Shared Google+   Circle: goo.gl/8XZbu
Thank You


Andreas Grunewald
http://grunewaldsrobots.com
Twitter: @agrunewald
Google+ http://goo.gl/IZzuS
http://www.linkedin.com/in/andreasgrunewald
http://about.me/agrunewald

Fighting Fragmentation with Fragments

  • 1.
    Fighting Fragmentation withFragments Optimizing Android apps for Honeycomb and beyond Andreas Grunewald Nov 2011 Barcamp Saskatoon
  • 2.
    Andreas Grunewald http://grunewaldsrobots.com goo.gl/b3bu6
  • 3.
    iOS Development is somuch more awesome than Android Developmemt
  • 4.
    Android is waytoo Fragmented
  • 5.
    Android is amess, say developers - Fortune
  • 6.
    Android Fragmentation: a real problem, developers say - Ubergizmo Developers: Android Fragmentation is a “Huge” Problem -Slashgear
  • 7.
    What problems doDevelopers see? 5. Ability to get paid. iOS leads here too, followed by BlackBerry. -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
  • 8.
    What problems doDevelopers see? 4. App visibility. "iOS continues to lead," Baird reports, "followed by Blackberry, with Android still receiving poor marks in this category." Developers are particularly concerned about the level of "junk" apps in the Android ecosystem. -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
  • 9.
    What problems doDevelopers see? 3. Ease of development. iOS outscored Android, but both were considered far easier to develop for than, say, Research in Motion's (RIMM) BlackBerry OS or Nokia (NOK) Symbian. -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
  • 10.
    What problems doDevelopers see? 2. Store fragmentation. Several developers expressed concern over Android app store fragmentation. "Generally," Baird reports, "developers seem to prefer a unified, single store experience like Apple's App Store." -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
  • 11.
    What problems doDevelopers see? 1. Device fragmentation. 56% of Android developers said that operating system fragmentation among the various Android devices was a meaningful or "huge" problem, a percentage that actually increased over the past three months. -Forbes http://tech.fortune.cnn.com/2011/04/04/android-is-a-mess-say-developers/
  • 12.
    Android Orphans: Visualizing aSad History of Support -theunderstatement http://theunderstatement.com/post/11982112928/android-orphans-visualizing-a-sad-history-of-support
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 19.
    Let's have alook... shall we?
  • 20.
    Platform Dashboard (Nov3rd) 2.2 & 2.3 together 85.1% of the market (With 2.1 included even 95.8%) Honeycomb only 1.9% Platform Codename API Level Distribution Android 1.5 Cupcake 3 0.9% Android 1.6 Donut 4 1.4% Android 2.1 Eclair 7 10.7% Android 2.2 Froyo 8 40.7% Android 2.3 - Gingerbread 9 0.5% Android 2.3.2 Android 2.3.3 - 10 43.9% Android 2.3.7 Android 3.0 Honeycomb 11 0.1% Android 3.1 12 0.9% Android 3.2 13 0.9%
  • 21.
    4.0 | 19.10 2011 3.2 | 15.07 3.1 | 10.05 3.0 | 22.02 Android Versions 2.3 | 06.12 2010 2.2 | 20.05 2.1 | 12.01 2.0 | 26.10 1.6 | 15.09 2009 1.5 | 30.04 1.1 | 09.02 1.0 | 23.09 2008
  • 22.
    Screen Sizes /Densities (Nov 3rd) 89.5% Normal Screen Size 73.8% hdpi + 23.7% mdpi = 97.5%
  • 23.
    So how doyou do it ?
  • 24.
    Fragments Introduced in Android3.0 Honeycomb. What about pre 3.0 Devices ?
  • 25.
    The Support Package akaCompatibility Library
  • 26.
    What's in it? ● Fragment ● FragmentManager ● ● FragmentTransaction ListFragment UI ● DialogFragment ● LoaderManager Loader Data ● ● AsyncTaskLoader ● CursorLoader
  • 27.
    Action Bar Android 3.0and up only, not in Support Package for pre 3.0 devices
  • 28.
    Fo Help is onthe way... kmr eo nG i tH ub http://actionbarsherlock.com
  • 29.
    Let's look atFragments again
  • 30.
    How do youstructure an App (The pre Honeycomb way)
  • 31.
    How do youstructure an App (Honeycomb and beyond)
  • 32.
    That doesn't lookmuch different!? vs
  • 33.
    Let's turn ourphone sideways
  • 34.
  • 35.
    And of courseTablet landscape
  • 36.
    So how manyactivites, fragments and layouts are there ?
  • 37.
    Put it alltogether
  • 38.
    So much forthe theory....
  • 39.
    Download the DemoApp from the Market http://goo.gl/y9C6c
  • 40.
    Patterns when usingFragments Event Listener Interface ● communication between Fragment and Activity ● separation of concerns / the fragments don't know about the activities -> easier to reuse across Activities Using FrameLayouts as placeholders and adding fragments dynamically ● allows hiding of the viewgroup ● Fragments can be given arguments when they are needed ● easier to refactor if no fragments are in layout.xml
  • 41.
    Event Listener Interface classMyActivity implements OnTitleListener class TitlesFragment extends ListFragment { public interface OnTitleListener { public void onTitleSelected(int titleId); } public void addOnTitleListener(OnTitleListener onTitleListener) { listener = onTitleListener; } public void onListItemClick(ListView l, View v, int position, long id) { ... listener.onTitleSelected(position); } ...
  • 42.
    FrameLayout Only use forstatic initialization and Fragments that are always present <fragment android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/titles" class="demo.gwr.fightfrag.fragments.TitlesFragment" android:layout_width="0px"></fragment> Use the framelayout for dynamic fragments, by hiding the Layout you can make the fragments appear when they are needed <FrameLayout android:id="@+id/chapters" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" android:visibility="gone" />
  • 43.
    FrameLayout FragmentManager fragMgr =getSupportFragmentManager(); ChaptersFragment chapters = (ChaptersFragment) fragMgr.findFragmentById(R.id.chapters); if (chapters == null){ chapters = ChaptersFragment.newInstance(titleId); chapters.addOnChapterListener(this); } FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.chapters, chapters); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); View chaptersFrame = findViewById(R.id.chapters); chaptersFrame.setVisibility(View.VISIBLE);
  • 44.
    Resources ● Android Developer Portal – SDK, Sample Code, Reference Docs http://developer.android.com ● Commonsware – Android Ebooks, Free Sample Code http://commonsware.com/ ● StackOverflow – Questions and Answers http://stackoverflow.com/questions/tagged/android ● Android IRC Channel #android-dev on freenode
  • 45.
    People Alexander Lucas Kirill Grouchnikov DevAdv might answer you on Stackoverflow Android User Interface Engineer Andy Rubin Matias Duarte The father of Android The creative mind behind Androids UI, Designed WebOS Chet Haase Reto Meier Android Graphics Engineer Author Professional Android Development Dan Morrill Romain Guy Android Engineer Android Graphics Engineer, Androdi Wallpapers Dianne Hackborn Roman Nurik Android Framework Engineer Android Developer Advocate Ed Burnette Tim Bray Author Hello Android Android Developer Advocate from Vancouver Jake Wharton Tor Norbye Developer of Action Bar Sherlock Works on the ADT & Eclipse Plugin Jean Baptiste Queru Xavier Ducrohet Android Open Source Project Android Developer Tools Lead Shared Google+ Circle: goo.gl/8XZbu
  • 46.
    Thank You Andreas Grunewald http://grunewaldsrobots.com Twitter:@agrunewald Google+ http://goo.gl/IZzuS http://www.linkedin.com/in/andreasgrunewald http://about.me/agrunewald