Android---ViewModelSavedState

此文章的代码是由LiveData和ViewModel演变过来,需要看前面的知识点。比如,导入依赖之类,布局…


用途:在进程被系统杀死时,数据能够得到保存。系统杀死不代表自己去杀死进程,而是异常杀死。比如,当你打开的程序很多。然后内存不够,系统就会自动的杀死一些进程。又或者屏幕的翻转。


第一种方法:
java:

public class MyDataBinding extends AppCompatActivity {

    private MyViewModel viewModel;
    private ActivityMyDataBindingBinding bindingBinding;

    public final static String KEY="KEY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        bindingBinding= DataBindingUtil.setContentView(this, R.layout.activity_my_data_binding);

        viewModel=new ViewModelProvider(this).get(MyViewModel.class);

        if(savedInstanceState!=null){
            viewModel.getNumber().setValue(savedInstanceState.getInt(KEY));
        }

        bindingBinding.setData(viewModel);//.setXxx(yyy)。Xxx表示在布局文件data标签的name
        bindingBinding.setLifecycleOwner(this);//如果没有这行,数据的改变不会被观察到
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY, viewModel.getNumber().getValue());
    }

}

ViewModel:

public class MyViewModel extends ViewModel {

    private MutableLiveData<Integer> Number;

    public MutableLiveData<Integer> getNumber() {
        if(Number==null){//这里代替了空参的构造方法
            Number=new MediatorLiveData<>();
            Number.setValue(0);
        }
        return Number;
    }
    public void addNumber(int n){
        Number.setValue(Number.getValue()+n);
    }
}

布局文件看:LiveData


由于上边的第一种方法算是一种老方法,所以引入了androidx的方法:

导入依赖:implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.0-alpha02'

ViewModel:

public class MyViewModel extends ViewModel {

    private SavedStateHandle handle;

    public MyViewModel(SavedStateHandle handle) {
        this.handle = handle;
    }

    public MutableLiveData<Integer> getNumber() {
        if(!handle.contains(MyDataBinding.KEY)){
            handle.set(MyDataBinding.KEY, 0);
        }
        return handle.getLiveData(MyDataBinding.KEY);
    }
    public void addNumber(int n){
        getNumber().setValue(getNumber().getValue()+n);
    }
}

java:

    private MyViewModel viewModel;
    private ActivityMyDataBindingBinding bindingBinding;

    public final static String KEY="KEY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        bindingBinding= DataBindingUtil.setContentView(this, R.layout.activity_my_data_binding);

        viewModel=new ViewModelProvider(this,new SavedStateViewModelFactory(getApplication(), this)).get(MyViewModel.class);

        if(savedInstanceState!=null){
            viewModel.getNumber().setValue(savedInstanceState.getInt(KEY));
        }
        bindingBinding.setData(viewModel);//.setXxx(yyy)。Xxx表示在布局文件data标签的name
        bindingBinding.setLifecycleOwner(this);//如果没有这行,数据的改变不会被观察到
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY, viewModel.getNumber().getValue());
    }

}

总结:
直接把这个看成map,值是MutableLiveData

 private SavedStateHandle handle;

你可能感兴趣的:(Android)