Share
Embed
Fragments Aren't The Only Way
A brief summary of alternatives to using Fragments for Android UI
Brandon Gogetap
October 12, 2016
Transcript
60 slides
-
What are Fragments? • “Represent a behavior or portion of
user interface in an Activity” -
What are Fragments? • “Represent a behavior or portion of
user interface in an Activity” • Used to compose a modular UI • One fragment may only be a piece of what the user sees on screen -
What are Fragments? • “Represent a behavior or portion of
user interface in an Activity” • Used to compose a modular UI • One fragment may only be a piece of what the user sees on screen • Receive lifecycle callbacks to allow state saving -
What are Fragments? • “Represent a behavior or portion of
user interface in an Activity” • Used to compose a modular UI • One fragment may only be a piece of what the user sees on screen • Receive lifecycle callbacks to allow state saving • Meant to be reusable across different activities -
What is the FragmentManager? • Maintains a history/back stack for
navigation • Allows you to replace/add fragments to the UI -
What is the FragmentManager? • Maintains a history/back stack for
navigation • Allows you to replace/add fragments to the UI • Can be used to find fragments that exist in the back stack -
Some developer.android.com suggestions • Your activity can call methods in
the fragment by acquiring a reference… -
Some developer.android.com suggestions • Your activity can call methods in
the fragment by acquiring a reference… • ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); -
Some developer.android.com suggestions • Your activity can call methods in
the fragment by acquiring a reference… • ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); • In some cases, you might need a fragment to share events with the activity… -
Some developer.android.com suggestions • Your activity can call methods in
the fragment by acquiring a reference… • ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); • In some cases, you might need a fragment to share events with the activity… • Create Interface that your Activity implements -
Some developer.android.com suggestions • Your activity can call methods in
the fragment by acquiring a reference… • ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); • In some cases, you might need a fragment to share events with the activity… • Create Interface that your Activity implements • Activity should coordinate communication between fragments -
Some developer.android.com suggestions public static class FragmentA extends ListFragment {
private OnArticleSelectedListener listener; // Container Activity must implement this interface public interface OnArticleSelectedListener { public void onArticleSelected(Uri articleUri); } @Override public void onAttach(Context context) { super.onAttach(context); try { listener = (OnArticleSelectedListener) context; } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement OnArticleSelectedListener"); } } } -
Some developer.android.com suggestions public static class FragmentA extends ListFragment {
private OnArticleSelectedListener listener; // Container Activity must implement this interface public interface OnArticleSelectedListener { public void onArticleSelected(Uri articleUri); } @Override public void onAttach(Context context) { super.onAttach(context); try { listener = (OnArticleSelectedListener) context; } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement OnArticleSelectedListener"); } } } -
Google suggestion summary • Activity A should coordinate communication between
Fragment A and B • Activity A has logic tied to Fragment B -
Google suggestion summary • Activity A should coordinate communication between
Fragment A and B • Activity A has logic tied to Fragment B • Activity B (containing Fragment B) should be used on phone-sized devices -
Google suggestion summary • Activity A should coordinate communication between
Fragment A and B • Activity A has logic tied to Fragment B • Activity B (containing Fragment B) should be used on phone-sized devices • Activity B has logic tied to Fragment B -
What are Fragments? • “Represent a behavior or portion of
user interface in an Activity” • Used to compose a modular UI • One fragment may only be a piece of what the user sees on screen • Receive lifecycle callbacks to allow state saving • Meant to be reusable across different activities -
What are Views? • “Represent a behavior or portion of
user interface in an Activity” • Used to compose a modular UI • One view may only be a piece of what the user sees on screen • Receive lifecycle callbacks to allow state saving • Meant to be reusable across different activities -
Custom views as UI building blocks • Pros • Simple
lifecycle • Promotes use of Presenter -
Custom views as UI building blocks • Pros • Simple
lifecycle • Promotes use of Presenter • Cons -
Custom views as UI building blocks • Pros • Simple
lifecycle • Promotes use of Presenter • Cons • No back stack API provided for you -
Custom views as UI building blocks • Implementing your own
back stack gives full control of API you’ll be using for, potentially, years -
Custom views as UI building blocks • Implementing your own
back stack gives full control of API you’ll be using for, potentially, years • You know when events are going to happen -
Custom views as UI building blocks • Implementing your own
back stack gives full control of API you’ll be using for, potentially, years • You know when events are going to happen • Fragment API history riddled with unpredictable callbacks -
Custom views as UI building blocks • Implementing your own
back stack gives full control of API you’ll be using for, potentially, years • You know when events are going to happen • Fragment API history riddled with unpredictable callbacks • IllegalStateException says Hi -
Custom views as UI building blocks • Implementing your own
back stack gives full control of API you’ll be using for, potentially, years • You know when events are going to happen • Fragment API history riddled with unpredictable callbacks • IllegalStateException says Hi • As with any in-house API, updates are under your control -
Security side note • isValidFragment • PreferenceActivity susceptible to “Fragment
Injection” https://securityintelligence.com/new-vulnerability-android-framework-fragment-injection/ -
Custom views as UI building blocks • Maybe not the
best choice for quick apps • Though you could make a library to be shared easily among different apps -
Custom views as UI building blocks • Maybe not the
best choice for quick apps • Though you could make a library to be shared easily among different apps • Longer initial implementation -
Custom views as UI building blocks • Maybe not the
best choice for quick apps • Though you could make a library to be shared easily among different apps • Longer initial implementation • Boilerplate -
Custom views as UI building blocks • BackStack class holds
Set of back stack objects -
Custom views as UI building blocks • BackStack class holds
Set of back stack objects • Activity attaches to specialized Presenter-type class which controls navigation -
Custom views as UI building blocks • BackStack class holds
Set of back stack objects • Activity attaches to specialized Presenter-type class which controls navigation • Activity handles creating views defined in BackStack items • Also in charge of animating views in/out -
Custom views as UI building blocks • BackStack • Contains
a Set of objects responsible for inflating Views • May define things such as animation/dagger scope • Ideally can be rebuilt from Bundle -
Custom views as UI building blocks • UI Manager class
(specialized Presenter) • Passes on push/pop transactions to Activity • The interface for interacting with the back stack for the rest of your app -
Conductor • Open source back stack management framework • Simple
lifecycle • Customizable transitions -
Conductor • Open source back stack management framework • Simple
lifecycle • Customizable transitions • Easy state persistence -
Conductor • Controllers • Building block of Conductor’s back stack
• Similar to Fragments in that they are wrappers around View inflation -
Conductor • Controllers • Building block of Conductor’s back stack
• Similar to Fragments in that they are wrappers around View inflation • Callbacks for permission results/activity result/etc. -
Conductor • Controllers • Building block of Conductor’s back stack
• Similar to Fragments in that they are wrappers around View inflation • Callbacks for permission results/activity result/etc. • Highly streamlined lifecycle -
Conductor • Router • Analog to FragmentManager • Coordinates the
pushing/popping of Controllers onto back stack -
Conductor • Router • Analog to FragmentManager • Coordinates the
pushing/popping of Controllers onto back stack • Transitions are defined in RouterTransactions -
Conductor • Overall, shares a similar pattern with Fragments/FragmentManager •
Main benefits • Simplified lifecycle • Easily customizable transitions -
Flow • Developed by Square • Back stack management composed
of “Keys” • Full control over back stack (easy to populate a back stack from scratch) • A good chunk of boilerplate required for setup -
Scoop • Developed by Lyft • Uses “Screen”s that define
“ViewController”s • Also gives full control over back stack history • Gives easy access to “scopes” to assist in dependency injection • Customizable transitions • Annotation-based