Fighting Fragmentation with Fragments

Post on 14-May-2015

3.587 views 2 download

Tags:

description

A presentation for Barcamp Saskatoon 2011. The demo app

Transcript of Fighting Fragmentation with Fragments

Fighting Fragmentation with FragmentsOptimizing Android apps for Honeycomb and beyond

Andreas GrunewaldNov 2011

Barcamp Saskatoon

Andreas Grunewaldhttp://grunewaldsrobots.com

goo.gl/b3bu6

iOS Development isso 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

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

CurrentiOS Devices

CurrentAndroid Devices

Are weARE 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 - 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%

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

Screen Sizes / Densities (Nov 3rd)

89.5% Normal Screen Size73.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 Packageaka Compatibility Library

What's in it ?

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

UI

Data

Action Bar

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

Help is on the way...

http://actionbarsherlock.com

Fork me on GitHub

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

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

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

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

Thank You

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