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

Ab4fa54bccd8073d0c0b4d4a2dd4193f?s=128

Nishant Srivastava

October 14, 2017
Tweet

Transcript

  1. None
  2. None
  3. @nisrulz #devfest17

  4. @nisrulz #devfest17

  5. @nisrulz #devfest17

  6. @nisrulz #devfest17

  7. @nisrulz #devfest17

  8. @nisrulz #devfest17

  9. @nisrulz #devfest17

  10. @nisrulz #devfest17

  11. @nisrulz #devfest17

  12. @nisrulz #devfest17

  13. @nisrulz #devfest17

  14. @nisrulz #devfest17

  15. @nisrulz #devfest17

  16. @nisrulz #devfest17

  17. @nisrulz #devfest17

  18. @nisrulz #devfest17

  19. @nisrulz #devfest17

  20. @nisrulz #devfest17

  21. @nisrulz #devfest17 •

  22. @nisrulz #devfest17 • •

  23. @nisrulz #devfest17 • • •

  24. @nisrulz #devfest17 • • • •

  25. @nisrulz #devfest17 • • • • •

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

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

    { url 'https://maven.google.com' } // or use google() } } @nisrulz #devfest17
  28. // 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
  29. @nisrulz #devfest17

  30. class MyLocationListener { public MyLocationListener(Context context, Callback callback) { //

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

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

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

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

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

    { super.onStart(); myLocationListener.start(); } public void onStop() { super.onStop(); myLocationListener.stop(); } } @nisrulz #devfest17
  36. 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
  37. 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
  38. @nisrulz #devfest17

  39. Lifecycle Owner Class with lifecycle i.e Activity & Fragments @nisrulz

    #devfest17
  40. Lifecycle Owner Class with lifecycle i.e Activity & Fragments Lifecycle

    Observer Class that observe lifecycle @nisrulz #devfest17
  41. @nisrulz #devfest17

  42. public class MyLocationListener implements LifecycleObserver { public MyLocationListener(Context context, Lifecycle

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

    lifecycle, Callback callback) { // ... lifecycle.addObserver(this); } } @nisrulz #devfest17
  44. 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
  45. 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
  46. public class MyLocationListener implements LifecycleObserver { // ... @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public

    void cleanup() { lifecycle.removeObserver(this); } } @nisrulz #devfest17
  47. public class MyLocationListener implements LifecycleObserver { // ... @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public

    void cleanup() { lifecycle.removeObserver(this); } } @nisrulz #devfest17
  48. class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public void

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

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

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

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

  53. 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
  54. @nisrulz #devfest17

  55. @nisrulz #devfest17

  56. @nisrulz #devfest17

  57. @nisrulz #devfest17

  58. @nisrulz #devfest17

  59. @nisrulz #devfest17

  60. @nisrulz #devfest17

  61. @nisrulz #devfest17

  62. @nisrulz #devfest17

  63. class LocationManager extends LiveData<Location>{ // ... } @nisrulz #devfest17

  64. 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
  65. 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
  66. class MyActivity extends AppCompatActivity { public void onCreate(...) { LocationManager.getInstance().observe(this,

    location -> { //Location Changed }) } } @nisrulz #devfest17
  67. class MyActivity extends AppCompatActivity { public void onCreate(...) { LocationManager.getInstance().observe(this,

    location -> { //Location Changed }) } } @nisrulz #devfest17
  68. @nisrulz #devfest17

  69. @nisrulz #devfest17

  70. @nisrulz #devfest17 It is the VM in MVVM architecture

  71. @nisrulz #devfest17 It is the VM in MVVM architecture A

    simple class to store and manage ui-related data
  72. @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
  73. @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)
  74. @nisrulz #devfest17

  75. @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 } }
  76. @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 } }
  77. @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 }); } }
  78. @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 }); } }
  79. @nisrulz #devfest17

  80. @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.
  81. @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
  82. @nisrulz #devfest17

  83. @nisrulz #devfest17

  84. @nisrulz #devfest17

  85. @nisrulz #devfest17

  86. @nisrulz #devfest17

  87. @nisrulz #devfest17

  88. @nisrulz #devfest17

  89. @nisrulz #devfest17

  90. @nisrulz #devfest17

  91. @nisrulz #devfest17

  92. @nisrulz #devfest17

  93. @nisrulz #devfest17

  94. @nisrulz #devfest17

  95. // app’s build.gradle // For Room, add: implementation "android.arch.persistence.room:runtime:1.0.0-beta2" annotationProcessor

    "android.arch.persistence.room:compiler:1.0.0-beta2" @nisrulz #devfest17
  96. @nisrulz #devfest17

  97. @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. }
  98. @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. }
  99. @nisrulz #devfest17 @Dao public interface UserDao { @Query("SELECT * FROM

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

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

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

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

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

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

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

    class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); }
  107. @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; } }
  108. @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; } }
  109. @nisrulz #devfest17 // .. INSTANCE= Room .databaseBuilder(getApplicationContext(), AppDatabase.class, "db_name") .build();

  110. @nisrulz #devfest17 // Get db AppDatabase appDatabase = AppDatabase .getDatabase(application);

    // Get data from db using the DAO List<User> userList = appDatabase .userDao() .getAll();
  111. @nisrulz #devfest17

  112. @nisrulz #devfest17 Compile time SQL statement verification

  113. @nisrulz #devfest17 Compile time SQL statement verification Testable

  114. @nisrulz #devfest17 Compile time SQL statement verification Testable Support for

    RxJava 2
  115. @nisrulz #devfest17 Compile time SQL statement verification Testable Support for

    RxJava 2 SQLite data relationship supported
  116. @nisrulz #devfest17 Compile time SQL statement verification Testable Support for

    RxJava 2 SQLite data relationship supported Easily define multiple indexes to improve query performance
  117. @nisrulz #devfest17

  118. @nisrulz #devfest17 @Dao public interface UserDao { @Query("SELECT * FROM

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

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

    user") LiveData<List<User>> getAll(); @Insert void insertAll(User... users); @Delete void delete(User user); }
  121. @nisrulz #devfest17

  122. @nisrulz #devfest17

  123. @nisrulz #devfest17 Data takes time to load

  124. @nisrulz #devfest17 Data takes time to load Performance lag

  125. @nisrulz #devfest17 Data takes time to load Performance lag Too

    much memory wasted
  126. @nisrulz #devfest17 Gradually load information as needed from a data

    source, without overloading the device or waiting too long for a big database query.
  127. @nisrulz #devfest17

  128. @nisrulz #devfest17

  129. // app’s build.gradle // For Paging, add: implementation "android.arch.paging:runtime:1.0.0-alpha3" @nisrulz

    #devfest17
  130. @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);
  131. @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)
  132. @nisrulz #devfest17 Implementation of RecyclerView.Adapter that presents data from a

    PagedLi
  133. @nisrulz #devfest17

  134. @nisrulz #devfest17

  135. @nisrulz #devfest17

  136. @nisrulz #devfest17

  137. None
  138. None