简介
简单的说就是用来监听Activity、Fragment等组件的生命周期变化。方便解耦,是google 官方推荐的架构组件。
两个重要的角色
- LifeCycleOwner 生命周期拥有者,即Activity与Fragment(也可自定义,但局限性大)
- LifeCycleObserver 生命周期观察者,可以是任何类,常见的有mvp的p,自定义View等
AppCompatActivity中的使用
AppCompatActivity与V4中的Fragment都已默认实现了LifeCyclerOwner接口,所以可以直接使用。但是Activity 并没有默认实现 LifecycleOwner 接口,下面会单独举个例子,如果想在activity中使用,我们应该怎么做。
声明周期观察者
// 1.集成 LifecycleObserver 接口
public class DemoLifeObserver implements LifecycleObserver {
final String TAG = "twomonth";
// 2.注解的方式监听声明周期变化
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
void create(){
Log.d(TAG,"Create");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void start(){
Log.d(TAG,"Start");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
void pause(){
Log.d(TAG,"Pause");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void stop(){
Log.d(TAG,"Stop");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void resume(){
Log.d(TAG,"Resume");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
void destroy(){
Log.d(TAG,"Destroy");
}
}
被观察者
public class LifeCycleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_life_cycle);
// 3.添加声明周期监听
getLifecycle().addObserver(new DemoLifeObserver());
}
}
这样做的好处显而易见,我们可以将监听生命周期变化的方法与组件进行解耦,代码架构上更加清晰,利于维护。
Activity 中使用
刚才我们提到过,AppCompatActivity 默认实现了LifeCycleOwner 接口,所以可以通过 addObserver 方法对监听这进行绑定,但是Activity 默认没有实现LifeCycleOwner。我们如果想在Activity中使用的话就要自己去实现。
// 1.实现 LifecycleOwner 接口
public class LifeCycle2Activity extends Activity implements LifecycleOwner {
private LifecycleRegistry lifecycleRegistry;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_life_cycle);
lifecycleRegistry = new LifecycleRegistry(this );
lifecycleRegistry.markState(Lifecycle.State.CREATED);
// 3、建立联系
getLifecycle().addObserver(new DemoLifeObserver());
}
// 2、实现getLifecycle 方法,返回注册的 lifecycleRegistery
@NonNull
@Override
public Lifecycle getLifecycle() {
return lifecycleRegistry;
}
}
Application 中使用,监听app 声明周期变化
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(new DemoLifeObserver());
}
}
==这里需要在gradle中添加依赖,implementation 'androidx.lifecycle:lifecycle-process:2.2.0'
在service中使用
public class MyService extends LifecycleService {
private DemoLifeObserver demoLifeObserver;
final String TAG = "mYservice";
public MyService() {
demoLifeObserver = new DemoLifeObserver();
getLifecycle().addObserver(demoLifeObserver);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
}
需要添加依赖:implementation "android.arch.lifecycle:extensions:1.1.1"