Fighting Fragmentation with Fragments

46
Fighting Fragmentation with Fragments Optimizing Android apps for Honeycomb and beyond Andreas Grunewald Nov 2011 Barcamp Saskatoon

description

A presentation for Barcamp Saskatoon 2011. The demo app

Transcript of Fighting Fragmentation with Fragments

Page 1: Fighting Fragmentation with Fragments

Fighting Fragmentation with FragmentsOptimizing Android apps for Honeycomb and beyond

Andreas GrunewaldNov 2011

Barcamp Saskatoon

Page 2: Fighting Fragmentation with Fragments

Andreas Grunewaldhttp://grunewaldsrobots.com

goo.gl/b3bu6

Page 3: Fighting Fragmentation with Fragments

iOS Development isso much more awesome than

Android Developmemt

Page 4: Fighting Fragmentation with Fragments

Android is way too Fragmented

Page 5: Fighting Fragmentation with Fragments

Android is a mess, say developers- Fortune

Page 6: Fighting Fragmentation with Fragments

Android Fragmentation: a real problem, developers say

- Ubergizmo

Developers: Android Fragmentation is a “Huge” Problem

-Slashgear

Page 7: Fighting Fragmentation with Fragments

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/

Page 8: Fighting Fragmentation with Fragments

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/

Page 9: Fighting Fragmentation with Fragments

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/

Page 10: Fighting Fragmentation with Fragments

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/

Page 11: Fighting Fragmentation with Fragments

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/

Page 12: Fighting Fragmentation with Fragments

Android Orphans: Visualizing a Sad History of Support

-theunderstatementhttp://theunderstatement.com/post/11982112928/android-orphans-visualizing-a-sad-history-of-support

Page 13: Fighting Fragmentation with Fragments

CurrentiOS Devices

Page 14: Fighting Fragmentation with Fragments

CurrentAndroid Devices

Page 15: Fighting Fragmentation with Fragments

Are weARE WE ED ?

Page 16: Fighting Fragmentation with Fragments

© http://www.americanprogress.org/cartoons/2008/08/081408.html

Page 17: Fighting Fragmentation with Fragments

© http://emilyandroid.deviantart.com/art/Yay-we-re-doomed-173226907

Page 18: Fighting Fragmentation with Fragments
Page 19: Fighting Fragmentation with Fragments

Let's have a look... shall we?

Page 20: Fighting Fragmentation with Fragments

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 - Android 2.3.2

Gingerbread 9 0.5%

Android 2.3.3 - Android 2.3.7

10 43.9%

Android 3.0 Honeycomb 11 0.1%

Android 3.1 12 0.9%

Android 3.2 13 0.9%

Page 21: Fighting Fragmentation with Fragments

Android Versions

2008 2009 2010 2011

1.0 | 23 .09

1.1 | 09 .02

1.5 | 30 .04

1.6 | 15 .092.0 | 26 .10

2.2 | 20 .05

2.3 | 06 .12

3.0 | 22 .02

2.1 | 12 .01

3.1 | 10 .053.2 | 15 .07

4.0 | 19 .10

Page 22: Fighting Fragmentation with Fragments

Screen Sizes / Densities (Nov 3rd)

89.5% Normal Screen Size73.8% hdpi + 23.7% mdpi = 97.5%

Page 23: Fighting Fragmentation with Fragments

So how do you do it ?

Page 24: Fighting Fragmentation with Fragments

Fragments

Introduced in Android 3.0 Honeycomb. What about pre 3.0 Devices ?

Page 25: Fighting Fragmentation with Fragments

The Support Packageaka Compatibility Library

Page 26: Fighting Fragmentation with Fragments

What's in it ?

● Fragment● FragmentManager● FragmentTransaction● ListFragment● DialogFragment● LoaderManager● Loader● AsyncTaskLoader● CursorLoader

UI

Data

Page 27: Fighting Fragmentation with Fragments

Action Bar

Android 3.0 and up only, not in Support Package for pre 3.0 devices

Page 28: Fighting Fragmentation with Fragments

Help is on the way...

http://actionbarsherlock.com

Fork me on GitHub

Page 29: Fighting Fragmentation with Fragments

Let's look at Fragments again

Page 30: Fighting Fragmentation with Fragments

How do you structure an App(The pre Honeycomb way)

Page 31: Fighting Fragmentation with Fragments

How do you structure an App(Honeycomb and beyond)

Page 32: Fighting Fragmentation with Fragments

That doesn't look much different!?

vs

Page 33: Fighting Fragmentation with Fragments

Let's turn our phone sideways

Page 34: Fighting Fragmentation with Fragments

How about Tablets ?

Page 35: Fighting Fragmentation with Fragments

And of course Tablet landscape

Page 36: Fighting Fragmentation with Fragments

So how many activites, fragments and layouts are there ?

Page 37: Fighting Fragmentation with Fragments

Put it all together

Page 38: Fighting Fragmentation with Fragments

So much for the theory....

Page 39: Fighting Fragmentation with Fragments

Download the Demo App from the Market

http://goo.gl/y9C6c

Page 40: Fighting Fragmentation with Fragments

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

Page 41: Fighting Fragmentation with Fragments

Event Listener Interfaceclass 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); }...

Page 42: Fighting Fragmentation with Fragments

FrameLayout

<FrameLayout android:id="@+id/chapters" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" android:visibility="gone" />

<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>

Only use for static initialization and Fragments that are always present

Use the framelayout for dynamic fragments, by hiding the Layout you can make the fragments appear when they are needed

Page 43: Fighting Fragmentation with Fragments

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);

Page 44: Fighting Fragmentation with Fragments

Resources

● Android Developer Portal – SDK, Sample Code, Reference Docshttp://developer.android.com

● Commonsware – Android Ebooks, Free Sample Codehttp://commonsware.com/

● StackOverflow – Questions and Answershttp://stackoverflow.com/questions/tagged/android

● Android IRC Channel#android-dev on freenode

Page 45: Fighting Fragmentation with Fragments

PeopleAlexander LucasDevAdv might answer you on Stackoverflow

Andy RubinThe father of Android

Chet HaaseAndroid Graphics Engineer

Dan MorrillAndroid Engineer

Dianne HackbornAndroid Framework Engineer

Ed BurnetteAuthor Hello Android

Jake WhartonDeveloper of Action Bar Sherlock

Jean Baptiste QueruAndroid Open Source Project

Shared Google+ Circle: goo.gl/8XZbu

Kirill GrouchnikovAndroid User Interface Engineer

Matias DuarteThe creative mind behind Androids UI, Designed WebOS

Reto MeierAuthor Professional Android Development

Romain GuyAndroid Graphics Engineer, Androdi Wallpapers

Roman NurikAndroid Developer Advocate

Tim BrayAndroid Developer Advocate from Vancouver

Tor NorbyeWorks on the ADT & Eclipse Plugin

Xavier DucrohetAndroid Developer Tools Lead

Page 46: Fighting Fragmentation with Fragments

Thank You

Andreas Grunewaldhttp://grunewaldsrobots.com Twitter: @agrunewaldGoogle+ http://goo.gl/IZzuShttp://www.linkedin.com/in/andreasgrunewaldhttp://about.me/agrunewald