Android Architecture Components 是谷歌在Google I/O 2017發佈一套幫助開發者解決Android 架構設計的方案。裏面包含了兩大塊內容:java
生命週期相關的 Lifecycle-aware Componentsreact
官方給予 Google 組件的功能:A collection of libraries that help you design robust, testable, and maintainable apps. Start with classes for managing your UI component lifecycle and handling data persistence。android
使用Google 提供的處理數據持久化和管理組件生命週期的類,有助於應用開發者們構建更加魯棒性,可測的,穩定可靠的應用。sql
提供主要的組件有:數據庫
Lifecycle:管理組件生命週期緩存
<figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption>網絡
在項目根目錄 build.gradle 文件添加倉庫依賴:數據結構
allprojects { repositories { jcenter() google() } }
若是遇到以下因 gradle 版本致使的編譯失敗問題:架構
Error:(6, 1) A problem occurred evaluating root project 'TestArc'.> Could not find method google() for arguments [] on repository container;
可修改成:app
maven { url 'https://maven.google.com' }
而後在主 module 的 build.gradle 文件添加須要依賴的組件:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' def lifecycle_version = "1.1.1" // ViewModel and LiveData compile "android.arch.lifecycle:extensions:$lifecycle_version" // alternatively - just ViewModel compile "android.arch.lifecycle:viewmodel:$lifecycle_version" // use -ktx for Kotlin // alternatively - just LiveData compile "android.arch.lifecycle:livedata:$lifecycle_version" // alternatively - Lifecycles only (no ViewModel or LiveData). // Support library depends on this lightweight import compile "android.arch.lifecycle:runtime:$lifecycle_version" annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // alternately - if using Java8, use the following instead of compiler compile "android.arch.lifecycle:common-java8:$lifecycle_version" // optional - ReactiveStreams support for LiveData compile "android.arch.lifecycle:reactivestreams:$lifecycle_version" // optional - Test helpers for LiveData // compile "android.arch.core:core-testing:$lifecycle_version" compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.facebook.stetho:stetho:1.3.1' // room compile 'android.arch.persistence.room:runtime:1.1.0' annotationProcessor 'android.arch.persistence.room:compiler:1.1.0' compile "android.arch.persistence.room:rxjava2:1.1.0" }
Lifecycle 組件指的是 android.arch.lifecycle 包下提供的各類類與接口,可讓開發者構建能感知其餘組件(主要指Activity 、Fragment)生命週期(lifecycle-aware)的類。
好比咱們須要監聽某個 Activity 生命週期的變化,在生命週期改變的時候打印日誌,通常作法構造回調的方式,先定義基礎 BaseActivityPresenter 接口:
public interface BaseActivityPresenter extends BasePresenter{ void onCreate(); void onStart(); void onResume(); void onPause(); void onStop(); void onDestroy(); }
在實現類中增長自定義操做(打印日誌):
public class ActivityPresenter implements BaseActivityPresenter { private static String TAG = ActivityPresenter.class.getSimpleName(); @Override public void onCreate() { LogUtil.i(TAG, "onCreate()"); } @Override public void onStart() { LogUtil.i(TAG, "onStart()"); } @Override public void onResume() { LogUtil.i(TAG, "onResume()"); } @Override public void onPause() { LogUtil.i(TAG, "onPause()"); } @Override public void onStop() { LogUtil.i(TAG, "onStop()"); } @Override public void onDestroy() { LogUtil.i(TAG, "onDestroy()"); } }
而後在須要監聽的 Activity 中依次回調方法:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBasePresenter = new ActivityPresenter(); } @Override protected void onStart() { super.onStart(); mBasePresenter.onStart(); } @Override protected void onResume() { super.onResume(); mBasePresenter.onResume(); } @Override protected void onPause() { super.onPause(); mBasePresenter.onPause(); } @Override protected void onStop() { super.onStop(); mBasePresenter.onStop(); } @Override protected void onDestroy() { super.onDestroy(); mBasePresenter.onDestroy(); }
在 Activity 的 onCreate() 方法中建立 BasePresenter,監聽 Activity 的生命週期方法。
上述寫能夠實現基礎的功能,可是不夠靈活,假如除了 ActivityPresenter 類,還有別的類要監聽 Activity 生命週期變化,那也須要添加許多生命週期的回調方法,比較繁瑣。那咱們是否能夠當 Activity 生命週期發生變化的時候主動通知需求方呢?答案就是使用 Lifecycle 提供的 LifecycleObserver:
public class ActivityLifeObserver implements BaseActivityPresenter, LifecycleObserver { private String TAG = ActivityLifeObserver.class.getSimpleName(); @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) @Override public void onCreate() { LogUtil.i(TAG, "onCreate()"); } @OnLifecycleEvent(Lifecycle.Event.ON_START) @Override public void onStart() { LogUtil.i(TAG, "onStart()"); } @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) @Override public void onResume() { LogUtil.i(TAG, "onResume()"); } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) @Override public void onPause() { LogUtil.i(TAG, "onPause()"); } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) @Override public void onStop() { LogUtil.i(TAG, "onStop()"); } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) @Override public void onDestroy() { LogUtil.i(TAG, "onDestroy()"); } }
讓咱們的業務類實現 ActivityLifeObserver 接口,同時在每個方法實現上增長 @OnLifecycleEvent(Lifecycle.Event.XXXX)註解,OnLifecycleEvent 對應了 Activity 的生命週期方法。被監聽的 Actiivty 實現 LifecycleOwner 接口,而後在須要監聽的 Activity 中註冊:
public class DetailActivity extends AppCompatActivity implements LifecycleOwner{ private static String TAG = DetailActivity.class.getSimpleName(); private LifecycleRegistry mLifecycleRegistry; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); mLifecycleRegistry = new LifecycleRegistry(this); // 註冊須要監聽的 Observer mLifecycleRegistry.addObserver(new ActivityLifeObserver()); mLifecycleRegistry.addObserver(new LocationLifeObserver()); } @Override public Lifecycle getLifecycle() { return mLifecycleRegistry; } }
運行以下:
com.troy.androidrc I/ActivityLifeObserver: onCreate() com.troy.androidrc I/ActivityLifeObserver: onStart() com.troy.androidrc I/ActivityLifeObserver: onResume() com.troy.androidrc I/ActivityLifeObserver: onPause() com.troy.androidrc I/ActivityLifeObserver: onStop() com.troy.androidrc I/ActivityLifeObserver: onDestroy()
其中 Lifecycle 使用兩個主要的枚舉類來表示其所關聯組件的生命週期:
Event 事件 從組件或者Lifecycle類分發出來的生命週期,它們和Activity/Fragment生命週期的事件一一對應。(ON_CREATE, ON_START, ON_RESUME, ON_PAUSE, ON_STOP, ON_DESTROY);
<figcaption style="margin: 10px 0px 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption>
LifecycleRegistry 類用於註冊和反註冊須要觀察當前組件生命週期的 Observer,用法以下:
// 初始化 mLifecycleRegistry = new LifecycleRegistry(this); mActivityLifeObserver = new ActivityLifeObserver(); // 註冊觀察者 mLifecycleRegistry.addObserver(mActivityLifeObserver); mLifecycleRegistry.addObserver(new LocationLifeObserver()); // 移除觀察者 mLifecycleRegistry.removeObserver(mActivityLifeObserver);
LiveData 是一種持有可被觀察數據的類(an observable data holder class)。和其餘可被觀察的類不一樣的是,LiveData是有生命週期感知能力的(lifecycle-aware,),這意味着它能夠在 activities, fragments, 或者 services 生命週期是活躍狀態時更新這些組件。
ViewModel 與 LiveData 之間的關係圖以下:
在 Activity 頁面有一 TextView,須要展現用戶 User 的信息,User 類定義:
public class User { public String userId; public String name; public String phone; @Override public String toString() { return "User{" + "userId='" + userId + '\'' + ", name='" + name + '\'' + ", phone='" + phone + '\'' + '}'; } }
常規的作法:
// 獲取 User 的數據後 mTvUser.setText(user.toString());
這樣作的一個問題,若是獲取或者修改 User 的來源不止一處,那麼須要在多個地方更新 TextView,而且若是在多處 UI 用到了 User,那麼也須要在多處更新。
使用 LiveData 與 ViewModel 的組合,將LiveData 持有 User 實體,做爲一個被觀察者,當 User 改變時,全部使用 User 的地方自動 change。構建一個 UserViewModel 以下:
public class UserViewModel extends ViewModel implements BaseViewModel<User> { private String TAG = UserViewModel.class.getSimpleName(); private MutableLiveData<User> liveUser; public MutableLiveData<User> getData(){ if(liveUser == null){ liveUser = new MutableLiveData<User>(); } liveUser.setValue(loadData()); return this.liveUser; } public void changeData(){ if(liveUser != null){ liveUser.setValue(loadData()); } } @Override public User loadData() { User user = new User(); user.userId = RandomUtil.getRandomNumber(); user.name = RandomUtil.getChineseName(); user.phone = RandomUtil.getRandomPhone(); LogUtil.i(TAG, "loadData(): " + user.toString()); return user; } @Override public void clearData() { } }
自定義的UserViewModel 繼承系統的 ViewModel,將 User 封裝成 MutableLiveData:if(liveUser == null){ liveUser = new MutableLiveData<User>(); }
在使用User 的地方增長觀察:
// view model.observe mUserViewModel = ViewModelProviders.of(this).get(UserViewModel.class); mUserViewModel.getData().observe(this, new Observer<User>() { @Override public void onChanged(@Nullable User user) { if(user != null){ mTvUser.setText(user.toString()); } } });
數據源發送改變的時候:
// 改變 User 內容 mButtonUser.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mUserViewModel != null && mUserViewModel.getData() != null){ mUserViewModel.changeData(); } } }); // setValue public void changeData(){ if(liveUser != null){ liveUser.setValue(loadData()); } }
這樣使用到 User 的地方,UI 會自動更新,日誌以下:
com.troy.androidrc I/DetailActivity: User{userId='9372622', name='鄧楠', phone='15607043749'} com.troy.androidrc I/DetailActivity: User{userId='6099877', name='文瑾慧', phone='13005794027'}
Room 持久層庫提供了一個方便咱們訪問 SQLite 數據庫的抽象層(an abstraction layer ),幫助咱們更好的在 APP 上建立咱們的數據緩存,可以讓 APP 即便在沒有網絡的狀況也能正常使用。
Room 的架構以下:
建立包含訂單表的數據庫以下步驟:
一、建立 Order.java:
@Entity(tableName = "orders") public class Order { @PrimaryKey @ColumnInfo(name = "order_id") public long orderId; @ColumnInfo(name = "address") public String address; @ColumnInfo(name = "owner_name") public String ownerName; @ColumnInfo(name = "owner_phone") public String ownerPhone; // 指示 Room 須要忽略的字段或方法 @Ignore public String ignoreText; @Embedded public OwnerAddress ownerAddress; }
二、建立 OrderDao:
@Dao public interface OrderDao { @Query("SELECT * FROM orders") List<Order> loadAllOrders(); @Insert void insertAll(Order... orders); @Query("SELECT * FROM orders WHERE order_id IN (:orderIds)") List<Order> queryOrderById(long[] orderIds); @Delete void deleteOrder(Order... orders); @Update void updateOrder(Order... orders); }
三、建立數據庫
@Database(entities = {Order.class, AddressInfo.class}, version = 2) public abstract class AppDatabase extends RoomDatabase{ public abstract OrderDao getOrderDao(); } // 實現類 public static void buildDb(){ DB_INSTANCE = Room. databaseBuilder(TroyApplication.getInstance(), AppDatabase.class, "troy_db") // 指定數據庫名稱 .addCallback(new RoomDatabase.Callback() { @Override public void onCreate(@NonNull SupportSQLiteDatabase db) { super.onCreate(db); // 數據庫建立回調; LogUtil.i(TAG, "onCreate"); } @Override public void onOpen(@NonNull SupportSQLiteDatabase db) { super.onOpen(db); // 數據庫使用回調; LogUtil.i(TAG, "onOpen"); } }) .allowMainThreadQueries() // 數據庫操做可運行在主線程 .build(); }
使用到的主要註解:
@Entity(tableName = "orders") // 定義表名;
@PrimaryKey // 定義主鍵;
@ColumnInfo(name = "order_id") // 定義數據表中的字段名;
@Ignore // 指示 Room 須要忽略的字段或方法;
@Embedded // 指定嵌入實體
@Query("SELECT * FROM orders") // 定義查詢數據接口;
@Insert // 定義增長數據接口;
@Delete // 定義刪除數據接口;
@Update // 定義更新數據接口;
增:
@Database(entities = {Order.class, AddressInfo.class}, version = 2) public abstract class AppDatabase extends RoomDatabase{ public abstract OrderDao getOrderDao(); } // 實現類 public static void buildDb(){ DB_INSTANCE = Room. databaseBuilder(TroyApplication.getInstance(), AppDatabase.class, "troy_db") // 指定數據庫名稱 .addCallback(new RoomDatabase.Callback() { @Override public void onCreate(@NonNull SupportSQLiteDatabase db) { super.onCreate(db); // 數據庫建立回調; LogUtil.i(TAG, "onCreate"); } @Override public void onOpen(@NonNull SupportSQLiteDatabase db) { super.onOpen(db); // 數據庫使用回調; LogUtil.i(TAG, "onOpen"); } }) .allowMainThreadQueries() // 數據庫操做可運行在主線程 .build(); }
刪:
// 一、刪除接口聲明 @Delete void deleteOrder(Order... orders); // 二、刪除接口實現 @Override public void deleteOrder(Order... orders) { __db.beginTransaction(); try { __deletionAdapterOfOrder.handleMultiple(orders); __db.setTransactionSuccessful(); } finally { __db.endTransaction(); } } // 三、刪除接口調用 AppDatabase db = DbManager.getDbInstance(); OrderDao orderDao = db.getOrderDao(); orderDao.deleteOrder(orderList.get(orderList.size() - 1));
改:
// 一、修改接口聲明 @Update void updateOrder(Order... orders); // 二、修改接口實現 @Override public void updateOrder(Order... orders) { __db.beginTransaction(); try { __updateAdapterOfOrder.handleMultiple(orders); __db.setTransactionSuccessful(); } finally { __db.endTransaction(); } } // 三、修改接口調用 AppDatabase db = DbManager.getDbInstance(); OrderDao orderDao = db.getOrderDao(); Order order = orderList.get(orderList.size() - 1); order.ownerName = "update - " + RandomUtil.getChineseName(); orderDao.updateOrder(order);
查:
// 一、查詢接口聲明 @Query("SELECT * FROM orders") List<Order> loadAllOrders(); // 二、查詢接口實現 @Override public List<Order> loadAllOrders() { final String _sql = "SELECT * FROM orders"; final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0); final Cursor _cursor = __db.query(_statement); try { final int _cursorIndexOfOrderId = _cursor.getColumnIndexOrThrow("order_id"); final int _cursorIndexOfAddress = _cursor.getColumnIndexOrThrow("address"); final int _cursorIndexOfOwnerName = _cursor.getColumnIndexOrThrow("owner_name"); final int _cursorIndexOfOwnerPhone = _cursor.getColumnIndexOrThrow("owner_phone"); final int _cursorIndexOfStreet = _cursor.getColumnIndexOrThrow("street"); final int _cursorIndexOfState = _cursor.getColumnIndexOrThrow("state"); final int _cursorIndexOfCity = _cursor.getColumnIndexOrThrow("city"); final int _cursorIndexOfPostCode = _cursor.getColumnIndexOrThrow("post_code"); final List<Order> _result = new ArrayList<Order>(_cursor.getCount()); while(_cursor.moveToNext()) { final Order _item; final Order.OwnerAddress _tmpOwnerAddress; if (! (_cursor.isNull(_cursorIndexOfStreet) && _cursor.isNull(_cursorIndexOfState) && _cursor.isNull(_cursorIndexOfCity) && _cursor.isNull(_cursorIndexOfPostCode))) { _tmpOwnerAddress = new Order.OwnerAddress(); _tmpOwnerAddress.street = _cursor.getString(_cursorIndexOfStreet); _tmpOwnerAddress.state = _cursor.getString(_cursorIndexOfState); _tmpOwnerAddress.city = _cursor.getString(_cursorIndexOfCity); _tmpOwnerAddress.postCode = _cursor.getInt(_cursorIndexOfPostCode); } else { _tmpOwnerAddress = null; } _item = new Order(); _item.orderId = _cursor.getLong(_cursorIndexOfOrderId); _item.address = _cursor.getString(_cursorIndexOfAddress); _item.ownerName = _cursor.getString(_cursorIndexOfOwnerName); _item.ownerPhone = _cursor.getString(_cursorIndexOfOwnerPhone); _item.ownerAddress = _tmpOwnerAddress; _result.add(_item); } return _result; } finally { _cursor.close(); _statement.release(); } } // 三、查詢接口調用 AppDatabase db = DbManager.getDbInstance(); OrderDao orderDao = db.getOrderDao(); return orderDao.loadAllOrders();
若是實體 Order 內部包含地址信息,地址信息分別包含 城市,郵政等信息,能夠這樣寫,使用@Embedded 註解:
static class OwnerAddress { public String street; public String state; public String city; @ColumnInfo(name = "post_code") public int postCode; } @Embedded public OwnerAddress ownerAddress;
數據查詢能夠返回 LiveData 數據:
@Query("SELECT * FROM orders") LiveData<List<Order>> loadAllOrderData();
經過 query 查詢返回的實體,能夠封裝成 對應RxJava 的操做符封裝對象,例如 Flowable,Maybe 等:
// 接口聲明 @Query("SELECT * from orders where order_id = :id LIMIT 1") Flowable<Order> queryOrderByIdV2(long id); @Query("SELECT * from orders where order_id = :id LIMIT 1") Maybe<Order> queryOrderByIdV3(long id); // 接口調用 private Maybe<Order> queryOrderV3(){ AppDatabase db = DbManager.getDbInstance(); OrderDao orderDao = db.getOrderDao(); return orderDao.queryOrderByIdV3(10001); } Maybe<Order> orderMaybe = queryOrderV3(); orderMaybe.subscribeOn(Schedulers.io()) .observeOn(Schedulers.newThread()) .subscribe(new Consumer<Order>() { @Override public void accept(@NonNull Order order) throws Exception { } });
以上代碼Demo 實現:
學會使用 Android Architecture Components 提供的組件簡化咱們的開發,可以使咱們開發的應用模塊更解耦更穩定,視圖與數據持久層分離,以及更好的擴展性與靈活性。
原做者:Boy·哈利波特