1.直接添加如下依赖就可以满足日常的工作,如果缺少哪个库,再去单独添加就好了
implementation "android.arch.lifecycle:extensions:1.1.1"
1 ackage com.example.lifecycledemo1; 2 3 import android.arch.lifecycle.Lifecycle; 4 import android.arch.lifecycle.LifecycleObserver; 5 import android.arch.lifecycle.OnLifecycleEvent; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.util.Log; 9 10 public class MainActivity extends AppCompatActivity { 11 12 private static final String TAG = "MainActivity"; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 getLifecycle().addObserver(new MyObserver());//1 19 } 20 21 public class MyObserver implements LifecycleObserver{ 22 23 @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) 24 void onResume(){ 25 Log.d(TAG, "Lifecycle call onResume"); 26 } 27 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) 28 void onPause(){ 29 Log.d(TAG, "Lifecycle call onPause"); 30 } 31 } 32 33 @Override 34 protected void onResume() { 35 super.onResume(); 36 Log.d(TAG, "onResume"); 37 } 38 39 @Override 40 protected void onPause() { 41 super.onPause(); 42 Log.d(TAG, "onPause"); 43 44 } 45 }
先实现MyObserver,对ON_CREATE和ON_RESUME事件进行监听。因为在Android Support Library 26.1.0 及其之后的版本,Activity和Fragment已经默认实现了LifecycleOwner接口,所以在注释1处可以直接使用getLifecycle方法获取Lifecycle对象,这样MyObserver就可以观察MainActivity的生命周期变化了,LifecycleOwner可以理解为被观察者,MainActivity默认实现了LifecycleOwner接口,也就是说MainActivity是被观察者。
运行程序,打印的log如下所示。
D/MainActivity: onResume D/MainActivity: Lifecycle call onResume D/MainActivity: Lifecycle call onPause D/MainActivity: onPause
3.MVP的例子
1 public class MyPresenter implements IPresenter { 2 private static final String TAG = "test"; 3 4 @Override 5 public void onResume() { 6 Log.d(TAG, "Lifecycle call onResume"); 7 } 8 9 @Override 10 public void onPause() { 11 Log.d(TAG, "Lifecycle call onPause"); 12 } 13 } 14 15 interface IPresenter extends LifecycleObserver { 16 17 @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) 18 void onResume(); 19 20 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) 21 void onPause(); 22 }
IPresenter接口继承自LifecycleObserver接口,MyPresenter又实现了IPresenter接口,这样MyPresenter成为了一个观察者。
接在在MainActivity中加入MyPresenter:
1 public class MainActivity extends AppCompatActivity { 2 3 private static final String TAG = "test"; 4 private IPresenter mPresenter; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 mPresenter = new MyPresenter(); 10 getLifecycle().addObserver(mPresenter); 11 } 12 13 @Override 14 protected void onResume() { 15 super.onResume(); 16 Log.d(TAG, "onResume"); 17 } 18 19 @Override 20 protected void onPause() { 21 super.onPause(); 22 Log.d(TAG, "onPause"); 23 24 } 25 }
MainActivity成为了被观察者,当它的生命周期发生变化时,MyPresenter就可以观察到,这样就不需要在MainActivity的多个生命周期方法中调用MyPresenter的方法了。
打印的日志如下:
D/test: onResume D/test: Lifecycle call onResume D/test: Lifecycle call onPause D/test: onPause