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

[Devfest '17] Architecture Components

[Devfest '17] Architecture Components

Get to know the new Architecture Components released by Google in I/O'17

Gave this talk for GDG Cloud Vancouver Devfest'17
Animated GIF version: https://goo.gl/dVj63L

Nishant Srivastava

October 14, 2017
Tweet

More Decks by Nishant Srivastava

Other Decks in Technology

Transcript

  1. // Project’s root build.gradle allprojects { repositories { jcenter() maven

    { url 'https://maven.google.com' } // or use google() } } @nisrulz #devfest17
  2. // Project’s root build.gradle allprojects { repositories { jcenter() maven

    { url 'https://maven.google.com' } // or use google() } } @nisrulz #devfest17
  3. // app’s build.gradle // For Lifecycles, LiveData, and ViewModel, add:

    implementation "android.arch.lifecycle:runtime:1.0.0" implementation "android.arch.lifecycle:extensions:1.0.0-beta2" annotationProcessor "android.arch.lifecycle:compiler:1.0.0-beta2" @nisrulz #devfest17
  4. class MyLocationListener { public MyLocationListener(Context context, Callback callback) { //

    ... } void start() { // connect to system location service } void stop() { // disconnect from system location service } } @nisrulz #devfest17
  5. class MyLocationListener { public MyLocationListener(Context context, Callback callback) { //

    ... } void start() { // connect to system location service } void stop() { // disconnect from system location service } } @nisrulz #devfest17
  6. class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public void

    onCreate(...) { myLocationListener = new MyLocationListener(this, (location) -> { // update UI }); } } @nisrulz #devfest17
  7. class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public void

    onCreate(...) { myLocationListener = new MyLocationListener(this, (location) -> { // update UI }); } } @nisrulz #devfest17
  8. class MyActivity extends AppCompatActivity { // .. public void onStart()

    { super.onStart(); myLocationListener.start(); } public void onStop() { super.onStop(); myLocationListener.stop(); } } @nisrulz #devfest17
  9. class MyActivity extends AppCompatActivity { // .. public void onStart()

    { super.onStart(); myLocationListener.start(); } public void onStop() { super.onStop(); myLocationListener.stop(); } } @nisrulz #devfest17
  10. class MyActivity extends AppCompatActivity { //... public void onStart() {

    super.onStart(); Util.checkUserStatus(result -> { // what if this callback is invoked AFTER activity is stopped? if (result) { myLocationListener.start(); } }); } // onStop() } @nisrulz #devfest17
  11. class MyActivity extends AppCompatActivity { //... public void onStart() {

    super.onStart(); Util.checkUserStatus(result -> { // what if this callback is invoked AFTER activity is stopped? if (result) { myLocationListener.start(); } }); } // onStop() } @nisrulz #devfest17
  12. Lifecycle Owner Class with lifecycle i.e Activity & Fragments Lifecycle

    Observer Class that observe lifecycle @nisrulz #devfest17
  13. public class MyLocationListener implements LifecycleObserver { public MyLocationListener(Context context, Lifecycle

    lifecycle, Callback callback) { // ... lifecycle.addObserver(this); } } @nisrulz #devfest17
  14. public class MyLocationListener implements LifecycleObserver { public MyLocationListener(Context context, Lifecycle

    lifecycle, Callback callback) { // ... lifecycle.addObserver(this); } } @nisrulz #devfest17
  15. public class MyLocationListener implements LifecycleObserver { // ... @OnLifecycleEvent(Lifecycle.Event.ON_START) public

    void start() { // connect to system location service } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void stop() { // disconnect from system location service } } @nisrulz #devfest17
  16. public class MyLocationListener implements LifecycleObserver { // ... @OnLifecycleEvent(Lifecycle.Event.ON_START) public

    void start() { // connect to system location service } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void stop() { // disconnect from system location service } } @nisrulz #devfest17
  17. class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public void

    onCreate() { myLocationListener = new MyLocationListener(this, getLifecycle(), location -> { //update UI }); Util.checkUserStatus(result -> { //... }); } } @nisrulz #devfest17
  18. class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public void

    onCreate() { myLocationListener = new MyLocationListener(this, getLifecycle(), location -> { //update UI }); Util.checkUserStatus(result -> { //... }); } } @nisrulz #devfest17
  19. class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public void

    onCreate() { myLocationListener = new MyLocationListener(this, getLifecycle(), location -> { //update UI }); Util.checkUserStatus(result -> { //... }); } } @nisrulz #devfest17
  20. class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public void

    onCreate() { myLocationListener = new MyLocationListener(this, getLifecycle(), location -> { //update UI }); Util.checkUserStatus(result -> { //... }); } } @nisrulz #devfest17
  21. As of release of Support Library v26.1.0 “Fragment and FragmentActivity

    (the base class for AppCompatActivity) now implement the LifecycleOwner interface from Architecture Components” @nisrulz #devfest17
  22. class LocationManager extends LiveData<Location>{ @override protected void onActive(){ super.onActive(); //

    connect to location } @override protected void onInactive(){ super.onInactive(); // disconnect from location } } @nisrulz #devfest17
  23. class LocationManager extends LiveData<Location>{ @override protected void onActive(){ super.onActive(); //

    connect to location } @override protected void onInactive(){ super.onInactive(); // disconnect from location } } @nisrulz #devfest17
  24. @nisrulz #devfest17 It is the VM in MVVM architecture A

    simple class to store and manage ui-related data
  25. @nisrulz #devfest17 It is the VM in MVVM architecture A

    simple class to store and manage ui-related data Data survives configuration changes such as screen rotation
  26. @nisrulz #devfest17 It is the VM in MVVM architecture A

    simple class to store and manage ui-related data Data survives configuration changes such as screen rotation No memory leaks (no references to activity or fragment or views)
  27. @nisrulz #devfest17 public class MyViewModel extends ViewModel { private MutableLiveData<List<User>>

    users; public LiveData<List<User>> getUsers() { if (users == null) { users = new MutableLiveData<List<Users>>(); loadUsers(); } return users; } private void loadUsers() { // do async operation to fetch users } }
  28. @nisrulz #devfest17 public class MyViewModel extends ViewModel { private MutableLiveData<List<User>>

    users; public LiveData<List<User>> getUsers() { if (users == null) { users = new MutableLiveData<List<Users>>(); loadUsers(); } return users; } private void loadUsers() { // do async operation to fetch users } }
  29. @nisrulz #devfest17 public class MyActivity extends AppCompatActivity { public void

    onCreate(Bundle savedInstanceState) { MyViewModel model = ViewModelProviders .of(this) .get(MyViewModel.class); model.getUsers().observe(this, users -> { // update UI }); } }
  30. @nisrulz #devfest17 public class MyActivity extends AppCompatActivity { public void

    onCreate(Bundle savedInstanceState) { MyViewModel model = ViewModelProviders .of(this) .get(MyViewModel.class); model.getUsers().observe(this, users -> { // update UI }); } }
  31. @nisrulz #devfest17 ViewModels - provide a convenient way to retain

    data across configuration changes - they are not persisted if the application is killed by the operating system.
  32. @nisrulz #devfest17 ViewModels - provide a convenient way to retain

    data across configuration changes - they are not persisted if the application is killed by the operating system. SavedInstanceState - stores data, usually ID - they are saved in system process memory, OS limits the amount
  33. @nisrulz #devfest17 @Entity public class User { @PrimaryKey private int

    uid; @ColumnInfo(name = "first_name") private String firstName; @ColumnInfo(name = "last_name") private String lastName; // Getters and setters are ignored for brevity, // but they're required for Room to work. }
  34. @nisrulz #devfest17 @Entity public class User { @PrimaryKey private int

    uid; @ColumnInfo(name = "first_name") private String firstName; @ColumnInfo(name = "last_name") private String lastName; // Getters and setters are ignored for brevity, // but they're required for Room to work. }
  35. @nisrulz #devfest17 @Dao public interface UserDao { @Query("SELECT * FROM

    user") List<User> getAll(); @Insert void insertAll(User... users); @Delete void delete(User user); }
  36. @nisrulz #devfest17 @Dao public interface UserDao { @Query("SELECT * FROM

    user") List<User> getAll(); @Insert void insertAll(User... users); @Delete void delete(User user); }
  37. @nisrulz #devfest17 @Dao public interface UserDao { @Query("SELECT * FROM

    user") List<User> getAll(); @Insert void insertAll(User... users); @Delete void delete(User user); }
  38. @nisrulz #devfest17 @Dao public interface UserDao { @Query("SELECT * FROM

    user") List<User> getAll(); @Insert void insertAll(User... users); @Delete void delete(User user); }
  39. @nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract

    class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); }
  40. @nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract

    class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); }
  41. @nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract

    class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); }
  42. @nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract

    class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); }
  43. @nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract

    class AppDatabase extends RoomDatabase { // .. private static AppDatabase INSTANCE; public static AppDatabase getDatabase(Context context) { if (INSTANCE == null) { INSTANCE= Room .databaseBuilder(getApplicationContext(), AppDatabase.class, "db_name") .build(); } return INSTANCE; } }
  44. @nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract

    class AppDatabase extends RoomDatabase { // .. private static AppDatabase INSTANCE; public static AppDatabase getDatabase(Context context) { if (INSTANCE == null) { INSTANCE= Room .databaseBuilder(getApplicationContext(), AppDatabase.class, "db_name") .build(); } return INSTANCE; } }
  45. @nisrulz #devfest17 // Get db AppDatabase appDatabase = AppDatabase .getDatabase(application);

    // Get data from db using the DAO List<User> userList = appDatabase .userDao() .getAll();
  46. @nisrulz #devfest17 Compile time SQL statement verification Testable Support for

    RxJava 2 SQLite data relationship supported Easily define multiple indexes to improve query performance
  47. @nisrulz #devfest17 @Dao public interface UserDao { @Query("SELECT * FROM

    user") List<User> getAll(); @Insert void insertAll(User... users); @Delete void delete(User user); }
  48. @nisrulz #devfest17 @Dao public interface UserDao { @Query("SELECT * FROM

    user") LiveData<List<User>> getAll(); @Insert void insertAll(User... users); @Delete void delete(User user); }
  49. @nisrulz #devfest17 @Dao public interface UserDao { @Query("SELECT * FROM

    user") LiveData<List<User>> getAll(); @Insert void insertAll(User... users); @Delete void delete(User user); }
  50. @nisrulz #devfest17 Gradually load information as needed from a data

    source, without overloading the device or waiting too long for a big database query.
  51. @nisrulz #devfest17 // If you use Room lib to manage

    your data @Query("select * from users WHERE age > :age order by name DESC, id ASC") TiledDataSource<User> usersOlderThan(int age);
  52. @nisrulz #devfest17 Loads data from a DataSource. You can configure

    how much data - is loaded at a time - should be prefetched (minimizing the amount of time your users have to wait for data to be loaded)