LifeCycles(让组件感知生命周期,Activity进一步解耦)

首先在build.gradle文件中添加一下依赖

android {
        dataBinding.enabled = true
        。。。
    }
    dependencies {
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-rc02'
    。。。
}

然后创建一个类继承ViewModel,在里面有有个SavedStateHandle类型的handle用来保存数据
MyViewModel.java

package com.mrlove.lifecycle;

import android.app.Application;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.SavedStateHandle;

public class MyViewModel extends AndroidViewModel {
    private SavedStateHandle handle;
    private static final String KEY_DATA = "key_data";

    public MutableLiveData<Long> getElapsedTime(long basetime){
            handle.set(KEY_DATA,SystemClock.elapsedRealtime()-basetime);
        return handle.getLiveData(KEY_DATA);
    }

    public MyViewModel(@NonNull Application application,SavedStateHandle handle) {
        super(application);
        this.handle = handle;
    }
}

定义一个实现生命感知的Chronometer
MyChronometer.java

package com.mrlove.lifecycle;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Chronometer;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class MyChronometer extends Chronometer implements LifecycleObserver {
    public MyViewModel myViewModel;
    private long elapsedTime;

    public MyChronometer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setMyViewModel(MyViewModel myViewModel){
        this.myViewModel = myViewModel;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)  //Lifecycle支持的监听生命周期的方式
    private void pauseMeter(){
          elapsedTime = myViewModel.getElapsedTime(getBase()).getValue();
          stop();
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private void resumeMeter(){
        setBase(SystemClock.elapsedRealtime()-elapsedTime);
        start();
    }
}

最后在mainactivity中实例化和绑定参数。
Mainactivity.java

package com.mrlove.lifecycle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import com.mrlove.lifecycle.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {
    MyViewModel myViewModel;
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        myViewModel = new ViewModelProvider(this,new SavedStateViewModelFactory(getApplication(),this)).get(MyViewModel.class);
        binding.myChronometer.setMyViewModel(myViewModel);
        binding.setData(myViewModel);
        getLifecycle().addObserver(binding.myChronometer); //通过此方法设置myChronometer的观察者
    }
}

总结:
通过融合之前的databinding,viewmodel,SavedStateHandle和现在自定义实现LifeCycles生命周期感知的myChronometer,让Activity进一步解耦,很多生命周期操作不用全部糅杂在Activity中,而是独立到各个组件当中。
github参考代码:https://github.com/Mrlove133481/Lifecycle

发布了27 篇原创文章 · 获赞 0 · 访问量 733
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览