Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fragments Aren't The Only Way

Fragments Aren't The Only Way

A brief summary of alternatives to using Fragments for Android UI

Avatar for Brandon Gogetap

Brandon Gogetap

October 12, 2016
Tweet

More Decks by Brandon Gogetap

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. What is the FragmentManager? • Maintains a history/back stack for

    navigation • Allows you to replace/add fragments to the UI
  5. 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
  6. 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);
  7. 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…
  8. 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
  9. 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
  10. 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");
 }
 }
 }
  11. 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");
 }
 }
 }
  12. Google suggestion summary • Activity A should coordinate communication between

    Fragment A and B • Activity A has logic tied to Fragment B
  13. 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
  14. 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
  15. 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
  16. 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
  17. Custom views as UI building blocks • Pros • Simple

    lifecycle • Promotes use of Presenter
  18. Custom views as UI building blocks • Pros • Simple

    lifecycle • Promotes use of Presenter • Cons
  19. Custom views as UI building blocks • Pros • Simple

    lifecycle • Promotes use of Presenter • Cons • No back stack API provided for you
  20. Custom views as UI building blocks • Implementing your own

    back stack gives full control of API you’ll be using for, potentially, years
  21. 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
  22. 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
  23. 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
  24. 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
  25. Security side note • isValidFragment • PreferenceActivity susceptible to “Fragment

    Injection” https://securityintelligence.com/new-vulnerability-android-framework-fragment-injection/
  26. 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
  27. 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
  28. 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
  29. Custom views as UI building blocks • BackStack class holds

    Set of back stack objects • Activity attaches to specialized Presenter-type class which controls navigation
  30. 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
  31. 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
  32. 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
  33. Conductor • Open source back stack management framework • Simple

    lifecycle • Customizable transitions • Easy state persistence
  34. Conductor • Controllers • Building block of Conductor’s back stack

    • Similar to Fragments in that they are wrappers around View inflation
  35. 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.
  36. 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
  37. Conductor • Router • Analog to FragmentManager • Coordinates the

    pushing/popping of Controllers onto back stack
  38. Conductor • Router • Analog to FragmentManager • Coordinates the

    pushing/popping of Controllers onto back stack • Transitions are defined in RouterTransactions
  39. Conductor • Overall, shares a similar pattern with Fragments/FragmentManager •

    Main benefits • Simplified lifecycle • Easily customizable transitions
  40. 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
  41. 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