PROBLEMS AGENDA ▸ Google I/O 2016 “What the Fragment” Recap. ▸ Fragment Best Practices ▸ Community reaction ▸ Square ▸ Fragmentless architecture ▸ Libraries!
logic is contained within each Fragment - not the Activity. ▸ Dynamic fragment created and added to our activity through FragmentManager. ▸ Through the FragmentManager, we have the ability to traverse fragments and apply animations to them.
which makes them difficult to explain. ▸ Google didn’t provide much guidance initially. ▸ Tricky API ▸ <fragment> tag ▸ Child Fragments ▸ FragmentManager vs ChildFragmentManager ▸ Additional Lifecycle…
Problem: Child Fragments would not be brought up to new state until after parent onCreate. (Fixed in Nougat) ▸ Problem: All transactions are executed right now. ▸ (Nougat) FragmentTransaction.commitNow()
the UI implement policy? ▸ CoordinatorLayout does a lot of fancy stuff, but calls out to higher component to enforce policy. ▸ Do you have to use the back button to navigate? ▸ Do you have to worry about state? ▸ Traditionally an issue with child fragments, but most of the inconsistencies have been fixed in Nougat.
depend on views, but not the other way around. ▸ They orchestrate views they sit above or coordinate with other components. ▸ Aware of external lifecycle.
are a lot of states, doesn’t mean you have to care about all of them. ▸ Yes, there are a lot of events and they are overwhelming. ▸ Think: ▸ “What minimum state will this component be in?”
logic and business logic, which makes unit testing difficult. ▸ Fragment Creation Magic ‣FooFragment.newInstance(bar); ‣DialogFragment dialogFragment = new DialogFragment() { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { ... } }; dialogFragment.show(fragmentManager, tag);
presenter of events. ▸ Allow the presenter to tell it what to do, but hide implementation from presenter. ▸ Doesn’t have to be the android.view.View class ▸ Could be a fragment that communicates with a presenter via a View Interface.
Presenter. ▸ Let the presenter tell the view what to do. ▸ Let the presenter know of any event that happens inside view. ▸ Button click, scroll event, ect… ▸ Having this contract allows for deeper unit testing.
▸ If using Fragment as presenter container (instead of Activity), you can still use FragmentManager. ▸ Roll your own. Can implemented as a simple Stack or List. ▸ Square’s Flow library ▸ Screen orientation changes and managing View State
ViewState preservation ▸ Used in conjunction with Flow for navigation ▸ ThirtyInch ▸ Presenters can survive the whole lifetime of an activity - only gets destroyed when Activity finishes ▸ Nucleus ▸ RxJava support
etc.) are simply entry points into your application. ▸ Android Framework tries (to a certain extent) to stay out of your way when it comes to architecture patterns. ▸ Dianne Hackborn’s (Android Framework Team) post on Android Architecture ▸ https://goo.gl/MZYvSF