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

Vasiliy Zukanov - Activities and Fragments are ...

Vasiliy Zukanov - Activities and Fragments are not MVxViews

droidcon Berlin

July 17, 2018
Tweet

More Decks by droidcon Berlin

Other Decks in Programming

Transcript

  1. The best place to hide a dead body is page

    2 of Google search results I saw pages 2, 3, 4…
  2. MVx in a nutshell: @VasiliyZukanov 6 Family of architectural patterns

    featuring separation of concerns for GUI applications Model : state and/or business logic and/or data structures View : application user interface x : flow control logic and/or state and/or business logic and/or data structures
  3. UI logic characteristics: Detailed and accurate requirements (UI mockups) Much

    higher rate of change in most cases Unreadable - verbose, messy, hacky, etc. Easiest to test manually Hardest to test automatically @VasiliyZukanov 14
  4. Outsourcing: @VasiliyZukanov 16 UI logic should be decoupled from the

    rest of the application Detailed UI specification MyActivity <<interface>> MyViewMvc Would you integrate this class into your app “as is”?
  5. The whole picture: @VasiliyZukanov 17 MyActivity <<interface>> MyViewMvc Life-cycle Screens

    navigation Runtime permissions Loaders Dependency injection Fragments Dialogs More… UI logic
  6. Activity is God Object with many responsibilities… … therefore, it’s

    impossible to decouple UI logic if it resides inside Activity!
  7. Extract UI logic into standalone class: @VasiliyZukanov 20 MyViewMvcImpl MyActivity

    <<interface>> MyViewMvc Composition instead of inheritance controller UI logic
  8. Activity only aware of simple interfaces: @VasiliyZukanov 21 public interface

    ViewMvc { View getRootView(); } public interface MyViewMvc extends ViewMvc { interface Listener { void onBuyClicked(); } void bindProduct(Product product); void registerListener(Listener listener); void unregisterListener(Listener listener); } setContentView(mViewMvc.getRootView())
  9. Pros: Decoupled UI logic (tremendous advantage) Modularity and reusability No

    need for ViewHolder in ListView More natural implementation of RecyclerView Same level of abstraction across the entire UI hierarchy @VasiliyZukanov 22
  10. 2010: Ivan proposed MVC on Android 2012: Josh realized that

    Activity is controller 2014: Vasiliy added explicit view interface The most mature architectural pattern for Android development!
  11. Summary: Extracting UI logic from Activities is the only way

    to make it truly decoupled MVx View is the natural abstraction level for UI logic in Android Activity can take on its natural role – controller Mature and documented[4] @VasiliyZukanov 28
  12. @VasiliyZukanov 30 Professional Android Unit Testing Android Dependency Injection with

    Dagger 2 Special coupon code: DROIDCON_DE My courses on Udemy
  13. References 1. Android Architecture: Message-based MVC - http://mindtherobot.com/blog/675/android- architecture-message-based-mvc/ 2.

    Android Architecture [in 10 parts] - http://www.therealjoshua.com/2011/11/android- architecture-part-1-intro/ 3. Android MVP for Beginners - https://android.jlelse.eu/android-mvp-for-beginners- 25889c500443 4. Activities in Android are not UI Elements - https://www.techyourchance.com/activities- android/ @VasiliyZukanov 33