Android Studio 之 数据存活【2】,返回桌面切换回来,内容还保存着
作者:互联网
使用ViewMode SavedState来使得ViewModel在系统杀死后台进程的时候得以存活。
本集使用到的库,需要在gradle中添加: implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01'
方法1:只用 savedInstanceState
package com.example.viewmodelrestore; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import androidx.lifecycle.SavedStateVMFactory; import androidx.lifecycle.ViewModelProviders; import com.example.viewmodelrestore.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { ActivityMainBinding binding; MyViewModel myViewModel; public final static String KEY_NUMBER = "my_number"; //常量 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 利用 ViewModel 进行保存数据,横屏切换后,数据都会保存 但是如果Activity 杀死后,重新打开,数据就没有了。 binding = DataBindingUtil.setContentView(this,R.layout.activity_main); //ViewModelProviders 需要在 build.gradle 中 添加如下引用: //implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0' myViewModel = ViewModelProviders.of(this).get(MyViewModel.class); binding.setData(myViewModel); binding.setLifecycleOwner(this); 这种是之前老的 api 中的方法 使用 onSaveInstanceState 键值对 ,进入后台,再回到app,数据还会显示之前的 新的方法是在 build.gradle 中引用 implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01' 在 MyViewModel 类中 添加一个构造函数 使用:SavedStateHandle handle SavedStateHandle.getLiveData */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); binding = DataBindingUtil.setContentView(this,R.layout.activity_main); //ViewModelProviders 需要在 build.gradle 中 添加如下引用: //implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0' //判断是否有数据,如果有直接取出来 //用 SaveInstanceState 就不需要用下面这个了 /* 【1】:老方法 myViewModel = ViewModelProviders.of(this).get(MyViewModel.class); if(savedInstanceState != null){ myViewModel.getNumber().setValue(savedInstanceState.getInt(KEY_NUMBER)); } */ //新方法【2】:直接用这句,就不需要判断为不为空了 //需要在 build.gradle 中引用 implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01' //注意创建 ViewModel 对象的时候,要带一个参数 : new SavedStateVMFactory(this) myViewModel = ViewModelProviders.of(this,new SavedStateVMFactory(this)).get(MyViewModel.class); binding.setData(myViewModel); binding.setLifecycleOwner(this); } //将数据保存起来 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(KEY_NUMBER,myViewModel.getNumber().getValue()); } }
ViewModel 类
package com.example.viewmodelrestore; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.SavedStateHandle; import androidx.lifecycle.ViewModel; public class MyViewModel extends ViewModel {
//【老方法】 private MutableLiveData<Integer> number;
//【新方法】 private SavedStateHandle handle; public MyViewModel(SavedStateHandle handle) { this.handle = handle; } public MutableLiveData<Integer> getNumber() { //【新方法】
//判断 handld 键值组里面有没有这个 if(!handle.contains(MainActivity.KEY_NUMBER)){ handle.set(MainActivity.KEY_NUMBER,0); } return handle.getLiveData(MainActivity.KEY_NUMBER);
/* 【老方法】,不用 MyViewModel 构造函数的情况下,不使用 SaveStateHandle if(number == null) { number = new MutableLiveData<>(); number.setValue(0); }
return number; */ }
public void add(){ //【新方法】
getNumber().setValue(getNumber().getValue() + 1);
//【老方法】
//number.setValue(number.getValue() + 1); } }
标签:handle,桌面,androidx,number,Studio,import,Android,MyViewModel,lifecycle 来源: https://www.cnblogs.com/gfwei/p/11780236.html