1.直接添加如下依賴就可以滿足日常的工作,如果缺少哪個庫,再去單獨添加就好了 添加這一句代碼就依賴瞭如下的庫。 2.先不談Activity和Fragment中如何使用,先舉一個Lifecycle的簡單例子。 先實現MyObserver,對ON_CREATE和ON_RESUME事件進行監聽。因為在An ...
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