5. public abstract class Lifecycle {
@MainThread
public abstract void addObserver(LifecycleObserver observer);
@MainThread
public abstract void removeObserver(LifecycleObserver observer);
@MainThread
public abstract State getCurrentState();
@SuppressWarnings("WeakerAccess")
public enum Event {
....
}
@SuppressWarnings("WeakerAccess")
public enum State {
....
}
}
5
6. public enum Event {
ON_CREATE,
ON_START,
ON_RESUME,
ON_PAUSE,
ON_STOP,
ON_DESTROY,
ON_ANY
}
public enum State {
DESTROYED,
INITIALIZED,
CREATED,
STARTED,
RESUMED;
public boolean isAtLeast(State state) {
return compareTo(state) >= 0;
}
}
6
9. public interface LifecycleOwner {
/**
* Returns the Lifecycle of the provider.
*
* @return The lifecycle of the provider.
*/
Lifecycle getLifecycle();
}
9
10. public class LifecycleActivity extends FragmentActivity implements LifecycleRegistryOwner {
private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);
public LifecycleActivity() {
}
public LifecycleRegistry getLifecycle() {
return this.mRegistry;
}
}
public class LifecycleFragment extends Fragment implements LifecycleRegistryOwner {
LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
public LifecycleFragment() {
}
public LifecycleRegistry getLifecycle() {
return this.mLifecycleRegistry;
}
}
10
11. public class LocationActivity extends LifecycleActivity {
private MyLocationListener myLocationListener;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myLocationListener = new MyLocationListener(getApplicationContext(),
getLifecycle(), location -> {
});
}
}
No more onStart and onStop
11
26. public abstract class ViewModel {
/**
* This method will be called when this ViewModel is no
longer used and will be destroyed.
* <p>
* It is useful when ViewModel observes some data and you
need to clear this subscription to
* prevent a leak of this ViewModel.
*/
@SuppressWarnings("WeakerAccess")
protected void onCleared() {
}
}
26
31. @Dao
interface FeedDao {
@Query("SELECT id, tiitle, subTitile FROM feed where title = :keyword")
List<Feed> loadFeed(String keyword);
}
@Entity
class Feed {
int id;
String title;
String subTitle;
}
31
@Database(entities = {Feed.class}, version 1)
abstract class MyDatabase extends RoomDatabase {
abstract FeedDao feedDao();
}
32. MyDatabase db = Room.databaseBuilder(getApplicationContext(),
MyDatabase.class)
.build();
db.feedDao().loadFeed(“xxxxx”);
32
33. @Query("SELECT id, tiitle, subTitile FROM Feeds where title = :keyword")
List<Feed> loadFeed(String keyword);
33
[SQLITE_ERROR] SQL error or missing database (no such table: feeds)
@Query("SELECT id, tiitle, subTitile FROM Feed where title = :keyword")
List<String> loadFeed(String keyword);
Error:(16, 18) error: Not sure how to convert a Cursor to this method’s return
type
36. UI Controller
(activities & fragments)
36
View Model
Repository
Data Sources
● Activities & Fragments
● Observers the ViewModel
● Keeps the UI up-to-date
● Forwards user Actions back to
the ViewModel
● Prepares & keeps the data for the
UI
● Includes LiveData, Observables
etc
● Survives configuration changes
● The gateway for the Ul Controller
● The complete data model for the
App
● Provides simple data modification
& retrieval APIs
● API to the data sources
● E.g Retrofit for a REST API
● Room or Share preferences
37. UI Controller (activities & fragments)
37
View Model
Repository
Data Sources
Mock View Model
LiveData
Android Instrumentation Test with Espresso