Android Architecture Components介紹
安卓架構組件是一套幫助開發(fā)者開發(fā)健壯,可測試,可維護的組件隔缀。
包含Handling Lifecycles,LiveData,ViewMode,Room.四個大方面闻镶,每個方面都可以單獨使用。本次文章主要寫第一個組件的簡單使用将鸵。后續(xù)慢慢寫完后面的內容勉盅。
Handling Lifecycles包含的內容
1.Lifecycle
2.LifecycleObserver
3.LifecycleOwner
Lifecycle簡單介紹
- 定義具有Android生命周期的對象
- v4包的Fragment與FragmentActivity類實現(xiàn)了LifecycleOwner接口,通過接口里面的getLifecycle()方法獲取Lifecycle對象顶掉。
- 我們可以在自己類里面實現(xiàn)LifecycleOwner接口草娜。
- 包含事件Events與狀態(tài)States
值得注意的是Android Architecture Components庫還沒正式發(fā)布。所以只有api 26.1.0及其之后的support library里面的Fragments和 Activities才實現(xiàn)了LifecycleOwner接口痒筒。如果是這之前的自定義的activities 和fragments需要實現(xiàn)LifecycleOwner 接口
LifecycleOwner簡單介紹
- A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.
- 具有Android生命周期的接口
- 可以處理生命周期帶來的變化卻不需要在Activity或者Fragment中實現(xiàn)任何代碼宰闰。
LifecycleObserver簡單介紹
- 這是一個接口,用于將一個類標記為LifecycleObserver
- 接口里面沒有任何方法簿透,而是依賴于 {@lOnLifecycleEvent}注釋方法移袍。
簡單示例
-
創(chuàng)建一個Activity與一個MyObserver類
- MainActivity主要代碼。
本例用的api版本小于26.1.0老充,所以MainActivity需要自己實現(xiàn)LifecycleOwner
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements LifecycleOwner{
//LifecycleRegistry 實現(xiàn)了Lifecycle
private LifecycleRegistry mLifecycleRegistry=new LifecycleRegistry(MainActivity.this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
MyObserver myObserver = new MyObserver();
mLifecycleRegistry.addObserver(myObserver);
}
@Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
}
- MyObserver代碼
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.OnLifecycleEvent;
import android.util.Log;
/**
* Created by tony.zhang
* Email: tony.zhang.Mr@foxmail.com
*/
public class MyObserver implements LifecycleObserver {
private static final String TAG="MyObserver";
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreated(){
Log.d(TAG, "onCreated: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart(){
Log.d(TAG, "onStart: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume(){
Log.d(TAG, "onResume: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
Log.d(TAG, "onPause: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop(){
Log.d(TAG, "onStop: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void customMethod(){
Log.d(TAG, "customMethod: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny(){//此方法可以有參數(shù)葡盗,但類型必須如兩者之一(LifecycleOwner owner,Lifecycle.Event event)
Log.d(TAG, "onAny: ");
}
}
- 打印消息
D/MyObserver: onCreated:
D/MyObserver: onAny:
D/MyObserver: onStart:
D/MyObserver: onAny:
D/MyObserver: onResume:
D/MyObserver: customMethod:
D/MyObserver: onAny:
- 退出Activity打印的消息
D/MyObserver: onPause:
D/MyObserver: onAny:
D/MyObserver: onStop:
D/MyObserver: onAny:
D/MyObserver: onAny:
- home鍵隱藏Activity打印消息
D/MyObserver: onPause:
D/MyObserver: onAny:
D/MyObserver: onStop:
D/MyObserver: onAny:
- 回到Activity打印消息
D/MyObserver: onStart:
D/MyObserver: onAny:
D/MyObserver: onResume:
D/MyObserver: customMethod:
D/MyObserver: onAny:
最后注意
- 在項目gradle文件里面添加maven { url 'https://maven.google.com' }
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}
- 在子模塊的gradle文件里添加library
compile 'android.arch.lifecycle:extensions:1.0.0-beta1'
compile 'android.arch.lifecycle:runtime:1.0.0'
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-beta1'
附錄傳送門
官方文檔: https://developer.android.com/topic/libraries/architecture/lifecycle.html#lc-bp
demo :https://github.com/Totomiz/AndroidArchitectureComponets