1. Add the following directly depend on to meet the daily work, what if the library is missing, go alone like added
implementation "android.arch.lifecycle:extensions:1.1.1"
Add this one dependent on the following code libraries.
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 }
First realize MyObserver, for ON_CREATE and ON_RESUME event monitoring. Because Android Support Library version 26.1.0 and later, Activity and Fragment already achieved LifecycleOwner default interface, so you can use in the comment at 1 getLifecycle method to get Lifecycle object directly, so MyObserver MainActivity can observe the life cycle of the change, LifecycleOwner can be understood as an observer, MainActivity default implementation LifecycleOwner interface that is MainActivity is an observer.
Running the program, log printed as follows.
D/MainActivity: onResume D/MainActivity: Lifecycle call onResume D/MainActivity: Lifecycle call onPause D/MainActivity: onPause
3.MVP examples
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 interface inherits from LifecycleObserver interface MyPresenter but also realized IPresenter interface so MyPresenter become an observer.
Then joining MyPresenter in MainActivity in:
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 become the observed, when its life cycle changes, MyPresenter can be observed, so that no method calls in a plurality of lifecycle MyPresenter MainActivity the method.
Print log is as follows:
D/test: onResume D/test: Lifecycle call onResume D/test: Lifecycle call onPause D/test: onPause