美文网首页
harmonyOS LifecycleView

harmonyOS LifecycleView

作者: CentForever | 来源:发表于2022-05-26 08:28 被阅读0次

自定义View添加Ability 生命周期监听
参考 https://www.jianshu.com/p/2e3f8a138ec0

package com.mgg.hmtest.view;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.Lifecycle;
import ohos.aafwk.ability.LifecycleStateObserver;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

// https://www.jianshu.com/p/2e3f8a138ec0
public class LifecycleView extends Component implements Component.BindStateChangedListener, LifecycleStateObserver {
    private static final String TAG = LifecycleView.class.getCanonicalName();
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00001, TAG);
    public LifecycleView(Context context) {
        super(context);
        initListener(context);
    }

    public LifecycleView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initListener(context);
    }

    public LifecycleView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initListener(context);
    }

    public LifecycleView(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        initListener(context);
    }

    private void initListener(Context context) {
        this.setBindStateChangedListener(this);
        if (context instanceof Ability) {
            ((Ability)context).getLifecycle().addObserver(this);
        }
    }

    @Override
    public void onComponentBoundToWindow(Component component) {
        // 绑定到window回调,这里可以获取component的一些具体宽高等属性
        HiLog.error(LABEL, "onComponentBoundToWindow");
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
        // 从window移除回调:这里可以对该component持有的一些资源进行释放
        // 比如该component显示在该界面需要执行动画,当从window移除后,可以停止动画释放资源等操作
        HiLog.error(LABEL, "onComponentUnboundFromWindow");
    }

    @Override
    public void onStateChanged(Lifecycle.Event event, Intent intent) {
        HiLog.error(LABEL, "onStateChanged:" + event.toString());
    }
}

05-25 20:03:34.391 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_ACTIVE
05-25 20:03:34.492 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onComponentBoundToWindow
05-25 20:04:30.576 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_INACTIVE
05-25 20:04:31.105 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_BACKGROUND
05-25 20:04:33.608 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_FOREGROUND
05-25 20:04:33.623 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_ACTIVE
05-25 20:04:35.319 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_INACTIVE
05-25 20:04:35.849 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_BACKGROUND
05-25 20:04:36.371 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_FOREGROUND
05-25 20:04:36.380 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_ACTIVE
05-25 20:04:37.477 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_INACTIVE
05-25 20:04:37.949 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_BACKGROUND
05-25 20:04:39.194 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_FOREGROUND
05-25 20:04:39.216 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_ACTIVE
05-25 20:05:39.373 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_INACTIVE
05-25 20:05:39.428 8834-8834/com.mgg.hmtest E 00001/com.mgg.hmtest.view.LifecycleView: onStateChanged:ON_BACKGROUND

相关文章

网友评论

      本文标题:harmonyOS LifecycleView

      本文链接:https://www.haomeiwen.com/subject/awkyprtx.html