之前我们说过用View Model可以保存数据,这是因为我们允许后台有多个进程,但是如果不运行后台有过个进程那怎么办?
如果后台杀死进程,要想数据保存,我们可以用 OnSaveInstanceState(Bundle outState)这个方法,将需要存的数按key-value值存入到outState,然后在onCreate()方法中取回。(但是这种解决方式比较旧)
代码如下:
package com.example.getnumber; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProviders; import android.os.Bundle; import com.example.getnumber.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { MyViewModel myViewModel; ActivityMainBinding binding; final static String KEY_NUMBER = "my_number"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this,R.layout.activity_main); myViewModel = ViewModelProviders.of(this).get(MyViewModel.class); // 第一次加载是空的 if(savedInstanceState!=null){ myViewModel.getNumber().setValue(savedInstanceState.getInt(KEY_NUMBER)); } binding.setData(myViewModel); binding.setLifecycleOwner(this); } @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); // 将 myViewModel里的值按key-value存入到outState outState.putInt(KEY_NUMBER,myViewModel.getNumber().getValue()); } }
view Model自带一个属性,我们可以用这个属性来保存数据。
首先把描黑的这句话(androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01)加入到第二个gradle的dependencies里。
代码:
MyViewModel
package com.example.getnumber; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.SavedStateHandle; import androidx.lifecycle.ViewModel; public class MyViewModel extends ViewModel { private SavedStateHandle handle;// 用来存放 // 此时会多增加了一个构造方法 public MyViewModel(SavedStateHandle handle){ // handle里存放一些key-value的数 this.handle = handle; } public MutableLiveData<Integer> getNumber() { // 如果handle里不包含叫KEY_NUMBER的值 if(!handle.contains(MainActivity.KEY_NUMBER)){ //我们就创建一个,这个发生在程序第一次运行,此时MainActivity.KEY_NUMBER是没有值的 handle.set(MainActivity.KEY_NUMBER,0); } // 以LiveData的形式返回 return handle.getLiveData(MainActivity.KEY_NUMBER); } public void add(){ getNumber().setValue(getNumber().getValue() 1); } }
Activity
package com.example.getnumber; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import androidx.lifecycle.SavedStateViewModelFactory; import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProviders; import android.os.Bundle; import com.example.getnumber.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { MyViewModel myViewModel; ActivityMainBinding binding; public final static String KEY_NUMBER = "my_number"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this,R.layout.activity_main); // 因为新增加的myViewModel构造函数带了一个参数,所以我们得把这个参数传进去 myViewModel = ViewModelProviders.of(this,new SavedStateViewModelFactory(getApplication(),this)).get(MyViewModel.class); binding.setData(myViewModel); binding.setLifecycleOwner(this); } }
如果是退出的话,则需要永久性保存数据,下篇博客会提到。