androidx中的lifecycle组件

2020-11-22 23:00:16

参考地址 androidx中的lifecycle组件

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));

    }
}




  • 2018-03-08 09:44:12

    前端性能监控:window.performance

    Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能,包括 Navigation Timing API和高分辨率时间数据。

  • 2018-03-08 09:44:15

    前端性能监控:window.performance

    Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能,包括 Navigation Timing API和高分辨率时间数据。

  • 2018-03-08 09:47:14

    ES6,Array.fill()函数的用法

    ES6为Array增加了fill()函数,使用制定的元素填充数组,其实就是用默认内容初始化数组。

  • 2018-03-08 09:53:39

    document.readyState

    一个document 的 Document.readyState 属性描述了文档的加载状态。

  • 2018-03-09 02:09:23

    ArrayBuffer:类型化数组

    ArrayBuffer对象、TypedArray对象、DataView对象是JavaScript操作二进制数据的一个接口。这些对象早就存在,属于独立的规格,ES6将它们纳入了ECMAScript规格,并且增加了新的方法。

  • 2018-03-09 11:45:11

    SQL SELECT DISTINCT 语句

    如需从 Company" 列中仅选取唯一不同的值,我们需要使用 SELECT DISTINCT 语句: