Fragments, the love story - Yonatan Levin, Gett

46
Fragments. Love story with IllegalStateException Droidcon TLV 2015

Transcript of Fragments, the love story - Yonatan Levin, Gett

Page 1: Fragments, the love story - Yonatan Levin, Gett

Fragments.Love story with IllegalStateException

Droidcon TLV 2015

Page 2: Fragments, the love story - Yonatan Levin, Gett

Yonatan Levin

Page 3: Fragments, the love story - Yonatan Levin, Gett

> 10M users

Ruby, Golang, Python,

Android, iOS

40 cities

Page 4: Fragments, the love story - Yonatan Levin, Gett

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

Page 5: Fragments, the love story - Yonatan Levin, Gett

Source Code

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

Page 6: Fragments, the love story - Yonatan Levin, Gett
Page 7: Fragments, the love story - Yonatan Levin, Gett

Fragments?

Own Layout Own behaviour Lifecycle Multiple

fragments

Re-Use Non - UI fragment

Close toActivity

Lifecycle

Page 8: Fragments, the love story - Yonatan Levin, Gett

Not really…

Page 9: Fragments, the love story - Yonatan Levin, Gett

Our app

git checkout 373f56d

Page 10: Fragments, the love story - Yonatan Levin, Gett

Structure

Page 11: Fragments, the love story - Yonatan Levin, Gett

Structure

Page 12: Fragments, the love story - Yonatan Levin, Gett

Structure

Page 13: Fragments, the love story - Yonatan Levin, Gett

Structure

Page 14: Fragments, the love story - Yonatan Levin, Gett

Structure

Page 15: Fragments, the love story - Yonatan Levin, Gett

Fragment transactions

Page 16: Fragments, the love story - Yonatan Levin, Gett

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

Page 17: Fragments, the love story - Yonatan Levin, Gett

UI Flags

git checkout 554221f

Page 18: Fragments, the love story - Yonatan Levin, Gett

First approach Solution

Page 19: Fragments, the love story - Yonatan Levin, Gett
Page 20: Fragments, the love story - Yonatan Levin, Gett

Solution?

Page 21: Fragments, the love story - Yonatan Levin, Gett
Page 22: Fragments, the love story - Yonatan Levin, Gett

LifeCycle

github.com/xxv/android-lifecycle

Page 23: Fragments, the love story - Yonatan Levin, Gett

UI Bus

git checkout 30e2df2

Page 24: Fragments, the love story - Yonatan Levin, Gett

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

Page 25: Fragments, the love story - Yonatan Levin, Gett

Why Otto?

No contextNo parcelableThreadStrong type

Page 26: Fragments, the love story - Yonatan Levin, Gett

UI Event Bus

Page 27: Fragments, the love story - Yonatan Levin, Gett

Logic

Page 28: Fragments, the love story - Yonatan Levin, Gett

Logic

Page 29: Fragments, the love story - Yonatan Levin, Gett

UI

Page 30: Fragments, the love story - Yonatan Levin, Gett

UI

Page 31: Fragments, the love story - Yonatan Levin, Gett

NestedFragments

Animating nested fragments is half-impossiblesetRetainInstance is inheritedonActivityResult is broken

Page 32: Fragments, the love story - Yonatan Levin, Gett

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

Complexity?

Page 33: Fragments, the love story - Yonatan Levin, Gett

WTFs/min = 2^fragment count.

Page 34: Fragments, the love story - Yonatan Levin, Gett

Custom Views

git checkout 0116bd8

Page 35: Fragments, the love story - Yonatan Levin, Gett

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

Page 36: Fragments, the love story - Yonatan Levin, Gett

MVP

View <-> Presenter <-> Model

Page 37: Fragments, the love story - Yonatan Levin, Gett

MainActivityView

Page 38: Fragments, the love story - Yonatan Levin, Gett

MainActivityView

Page 39: Fragments, the love story - Yonatan Levin, Gett

Presenter

Page 40: Fragments, the love story - Yonatan Levin, Gett

Presenter - cont

Page 41: Fragments, the love story - Yonatan Levin, Gett

DialogView

Page 42: Fragments, the love story - Yonatan Levin, Gett

DialogView - cont

Page 43: Fragments, the love story - Yonatan Levin, Gett

Model

Page 44: Fragments, the love story - Yonatan Levin, Gett

What is missing here

Backstack: use FlowSaveInstanceState: Mortar

Page 45: Fragments, the love story - Yonatan Levin, Gett

So what we got?

Decoupled layers

Easy to debug

Fully testable business logic, data model

IllegalStateException Safe