class MyLocationListener { public MyLocationListener(Context context, Callback callback) { // ... } void start() { // connect to system location service } void stop() { // disconnect from system location service } } @nisrulz #devfest17
class MyLocationListener { public MyLocationListener(Context context, Callback callback) { // ... } void start() { // connect to system location service } void stop() { // disconnect from system location service } } @nisrulz #devfest17
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
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
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
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
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
@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
@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)
@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.
@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
@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. }
@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. }
@nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); }
@nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); }
@nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); }
@nisrulz #devfest17 @Database(entities = {User.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); }
@nisrulz #devfest17 // Get db AppDatabase appDatabase = AppDatabase .getDatabase(application); // Get data from db using the DAO List userList = appDatabase .userDao() .getAll();
@nisrulz #devfest17 Compile time SQL statement verification Testable Support for RxJava 2 SQLite data relationship supported Easily define multiple indexes to improve query performance
@nisrulz #devfest17 Gradually load information as needed from a data source, without overloading the device or waiting too long for a big database query.
@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 usersOlderThan(int age);
@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)