Fragments, the love story - Yonatan Levin, Gett

Preview:

Citation preview

Fragments.Love story with IllegalStateException

Droidcon TLV 2015

Yonatan Levin

> 10M users

Ruby, Golang, Python,

Android, iOS

40 cities

http://www.meetup.com/TLV-Android-Academy/

Source Code

https://github.com/parahall/fragment-illegal-solution

Fragments?

Own Layout Own behaviour Lifecycle Multiple

fragments

Re-Use Non - UI fragment

Close toActivity

Lifecycle

Not really…

Our app

git checkout 373f56d

Structure

Structure

Structure

Structure

Structure

Fragment transactions

Qu'est-ce que c'est IllegalStateException?

Activity save it’s state something in between onPause() and onStop()

Attempt to commit a FragmentTransaction after the activity's state had been saved

UI Flags

git checkout 554221f

First approach Solution

Solution?

LifeCycle

github.com/xxv/android-lifecycle

UI Bus

git checkout 30e2df2

UI BusImplementation main goal:

- Separate Business logic from Activity/Fragments- Fire event on UI Event Bus (Otto)- UI components registered only in between OnResume and OnPause

Why Otto?

No contextNo parcelableThreadStrong type

UI Event Bus

Logic

Logic

UI

UI

NestedFragments

Animating nested fragments is half-impossiblesetRetainInstance is inheritedonActivityResult is broken

Have you ever tried to write test for your UI logic?

Complexity?

WTFs/min = 2^fragment count.

Custom Views

git checkout 0116bd8

Square approach - Custom ViewPush the logic to the views and inflate the right view.

MVP pattern will help to control it and hold business related logic.

More libraries to help backstack and saveInstanceState

MVP

View <-> Presenter <-> Model

MainActivityView

MainActivityView

Presenter

Presenter - cont

DialogView

DialogView - cont

Model

What is missing here

Backstack: use FlowSaveInstanceState: Mortar

So what we got?

Decoupled layers

Easy to debug

Fully testable business logic, data model

IllegalStateException Safe

levinyon@gmail.comhttps://plus.google.com/+YonatanLevin