Lifecycle-aware components生命周期感知组件执行操作,以响应另一个组件生命周期状态的更改,例如Activity和Fragment。这些组件可以帮助您生成更有组织、更容易维护的轻量级代码。
没有生命周期感知组件,你的代码可能是这样的
class MyLocationListener { public MyLocationListener(Context context, Callback callback) { // ... } void start() { // connect to system location service } void stop() { // disconnect from system location service }}class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; @Override public void onCreate(...) { myLocationListener = new MyLocationListener(this, (location) -> { // update UI }); } @Override public void onStart() { super.onStart(); myLocationListener.start(); // manage other components that need to respond // to the activity lifecycle } @Override public void onStop() { super.onStop(); myLocationListener.stop(); // manage other components that need to respond // to the activity lifecycle }}
Android框架中定义的大多数应用程序组件都具有附加的生命周期。生命周期由运行在流程中的操作系统或框架代码管理。它们是Android工作原理的核心,您的应用程序必须尊重它们。
不这样做可能会引发内存泄漏,甚至应用程序崩溃。
lifecycle使用
1,让需要感知生命周期的组件实现LifecycleObserver接口
import androidx.lifecycle.Lifecycle;import androidx.lifecycle.LifecycleObserver;import androidx.lifecycle.OnLifecycleEvent;/** * @author MengK */public class MyLocationListener implements LifecycleObserver { private Context context; public MyLocationListener(Context context) { this.context = context; } @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) void onCreate() { showLog("onCreate"); } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) void onDestroy() { showLog("onDestroy"); } @OnLifecycleEvent(Lifecycle.Event.ON_START) void onStart() { showLog("onStart"); } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) void onStop() { showLog("onStop"); } @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) void onResume() { showLog("onResume"); } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) void onPause() { showLog("onPause"); } private void showLog(String msg) { Log.e("===MyLocationListener", msg); }}
2,对应的activity或者fragment实现LifecycleOwner,并调用getLifecycle().addObserverLifecycle().addObserver添加观察者
import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.LifecycleOwner; import android.os.Bundle; /** * @author MengK */ public class MainActivity extends AppCompatActivity implements LifecycleOwner { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getLifecycle().addObserver(new MyLocationListener(this)); // MyCommpont myCommpont = new MyCommpont(); // myCommpont.onCreate(); // myCommpont.onStart(); // myCommpont.getLifecycle().addObserver(new MyLocationListener(this)); } }
打印结果
2019-09-01 14:53:13.956 22701-22701/com.mengk.lifecyclerdemo E/===MyLocationListener: onCreate 2019-09-01 14:53:13.960 22701-22701/com.mengk.lifecyclerdemo E/===MyLocationListener: onStart 2019-09-01 14:53:13.968 22701-22701/com.mengk.lifecyclerdemo E/===MyLocationListener: onResume 2019-09-01 14:53:16.678 22701-22701/com.mengk.lifecyclerdemo E/===MyLocationListener: onPause 2019-09-01 14:53:17.282 22701-22701/com.mengk.lifecyclerdemo E/===MyLocationListener: onStop 2019-09-01 14:53:19.258 22701-22701/com.mengk.lifecyclerdemo E/===MyLocationListener: onDestroy
自定义生命周期组件
如果你有一个自定义类,你想让它成为LifecycleOwner,你可以使用LifecycleRegistry类,但是你需要将事件转发到该类中,如下面的代码示例所示:
import androidx.annotation.NonNull;import androidx.lifecycle.Lifecycle;import androidx.lifecycle.LifecycleOwner;import androidx.lifecycle.LifecycleRegistry;/** * @author Mengk * @date {2019/9/1} * @description */public class MyCommpont implements LifecycleOwner { private LifecycleRegistry lifecycleRegistry; public MyCommpont() { lifecycleRegistry = new LifecycleRegistry(this); } public void onCreate() { lifecycleRegistry.markState(Lifecycle.State.CREATED); } public void onStart() { lifecycleRegistry.markState(Lifecycle.State.STARTED); } public void onKillApp() { lifecycleRegistry.markState(Lifecycle.State.DESTROYED); } @NonNull @Override public Lifecycle getLifecycle() { return lifecycleRegistry; }}
调用
/** * @author MengK */ public class MainActivity extends AppCompatActivity /*implements LifecycleOwner*/ { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // getLifecycle().addObserver(new MyLocationListener(this)); MyCommpont myCommpont = new MyCommpont(); myCommpont.onCreate(); myCommpont.onStart(); myCommpont.getLifecycle().addObserver(new MyLocationListener(this)); } }