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

Winning with Android Architecture

Winning with Android Architecture

Avatar for Nosakhare Belvi

Nosakhare Belvi

November 24, 2018

More Decks by Nosakhare Belvi

Other Decks in Technology

Transcript

  1. Android Architecture Component Why : What: • Best practices and

    guidelines for building Android Apps • Solves the build problem of lifecycle • Independent Set of Components • Very powerful when used all together • Recommended way/pattern of building Android apps
  2. // in your project level build,gradle allProjects{ repositories{ jcenter() google()

    } } // In your app level build.gradle // For LifeCycles , ViewModels, LiveData compile “android.arch.lifecycle:runtime:1.1.1” compile “android.arch.lifecycle:extensions.1.1” // Others can also go here Add Architecture Component to your app
  3. // Without LifeData @Query (“SELECT * FROM” + TABLE )

    List<Movie> fetchData(); // With LifeData @Query (“SELECT * FROM” + TABLE ) LiveData<List<Movie>> fetchData(); Wrap response object into a LiveData Yo h e c t i w ve wa t c re t n e //observe on the UI repository.fetchData().observe(this, new Observer<List<Item>>(){ @Override public void onChange(@Nullable List<Item> item){ // stream of changes } });
  4. What if... • Configuration change • User Behaviour • Add

    anything that bothers you about lifecycle here
  5. #DevFest2018 ViewModel • Data Survives configuration changes • No leaks

    (no reference to activities,fragment or views) • Store and manage UI related data
  6. Worthy of Notes: #DevFest2018 • VMs can be shared between

    UI Components • VMs are not persisted if the application is killed by the OS • Use Sparingly - Don’t o l a t mu d
  7. Create a ViewModel public class ItemViewModel extends ViewModel{ private LiveData<List<Item>>

    items; public ItemViewModel(){ // Do not work on the main thread items = ItemRepository.getInstance().loadAll(); } } public LiveData<List<Event>> getItems() { return items};
  8. Get the ViewModel public void Oncreate(Bundle savedInstanceState){ super.Oncreate(sacedInstanceState); // Get

    the VM. // this ties this View model to the lifeCycler. It refers to the lifeCycle Owner // in this case, an Activity. viewModel = ViewModelProdiders.of(this).get(ItemViewModel.class); }
  9. LifeCycle & LifeCycle Aware Components • Interfaces that let you

    build aware component • Automatically adjust behavior according to the current lifecycle • Helps avoid memory leaks and complex activity/fragment code
  10. Handling LifeCycle public class ItemActivity extends AppCompactActivity { public void

    OnStart(){ videoPlayer.start(); // initialise other items } public void OnStart(){ videoPlayer.start(); // clear up resources } } public class ItemComponent implements LifeCycleObserver { @OnLifeCycleEvent(Lifecycle.Event.On_START) public void OnStart(){ // do something } // OR DO THIS public void enable(){ if (lifecycle.getState().isAtLeast(STARTED)){ // do something } } } The y The O Way
  11. Add observe to a LifecycleOwner public class ItemActivity extends LifecycleActivity

    { @Override public void OnCreate(@Nullable Bundle savedInstanceState){ super.onCreate(savedInstanceState); getLifeCycle().addObserver(new ItemComponent()); } }
  12. Example of a Lifecycle Owner public class ItemActivity extends LifecycleActivity

    { @Override public void OnCreate(@Nullable Bundle savedInstanceState){ super.onCreate(savedInstanceState); getLifeCycle().addObserver(new ItemComponent()); } }
  13. • Architecture Component are a welcome addition to Android •

    Helps solve complex problem especially with handling lifecycles management • Helps mitigate memory leaks and exceptions due to being in the incorrect state • Simple, readable and maintainable code.
  14. #DevFest2018 Quick Reminder They can work individually and they mix

    well together. Use these arsenals to build better apps.
  15. Best Coding Practices #DevFest2018 • Avoid work when possible ◦

    Use recommended libraries (also contribute) • Profile your code • Use Support Libraries Features • Minimize memory consumption Al a s R be • Dev ar o r -co t ed • Bat y e s ti • Ban d s ec u • Use p i c a t
  16. Catching up with Android Trends #DevFest2018 • Follow the right

    people on twitter (especially experts like Jake Wharton, Romain Guy, Ian Lake, Chris Bane) • Android developer blog • Android developer site • Android Weekly • Watch talks from Google.io (If you weren’t invited or can’t afford to go) • Follow Android Developer Channel on Youtube • Android dev summit (livestream or watch videos after summit) • Popular Github repo or projects from rockstar devs on Android. • Blogs of Android dev Experts e.g ProAndroidDev, castle.io
  17. Helpful Resources #DevFest2018 • https://www.youtube.com/watch?v=IrMw7MEgADk • https://www.youtube.com/watch?v=iDH84_OMPmw • https://www.youtube.com/watch?v=7p22cSzniBM •

    https://proandroiddev.com/mvvm-architecture-viewmodel-and-livedata-part-1-604f50cda1 • https://developer.android.com/studio/profile/android-profiler?gclid=CjwKCAiAiarfBRASEiwAw1tYv-z3dH8CMm C7c0AErrP_CgXoATVDI8HC8rtmwmsiXYfwlt29gwMLsBoC6nMQAvD_BwE • https://developer.android.com/jetpack/ • https://developer.android.com/topic/libraries/architecture/ • https://www.youtube.com/watch?v=7ll-rkLCtyk • https://github.com/uber/okbuck