Android Studio of survival data [2], return to the desktop switch back, content is still preserved

Use ViewMode SavedState to make ViewModel survived when the system kill background processes.

This set of libraries to use, the need to add gradle: implementation 'androidx.lifecycle: lifecycle-viewmodel-savedstate: 1.0.0-alpha01'

 

Method 1: only  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 using saved data, the horizontal screen switching, data will be stored
    But if Activity kill, re-open, there is no data.

        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        // ViewModelProviders need to add build.gradle in the following references:
        //implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
        myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
        binding.setData(myViewModel);
        binding.setLifecycleOwner(this);
        This method is before the old api

        OnSaveInstanceState use key-value pairs, into the background, and then back to the app, data will be displayed before
        The new method is referenced in the build.gradle implementation 'androidx.lifecycle: lifecycle-viewmodel-savedstate: 1.0.0-alpha01'
        Add a class constructor 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 need to add build.gradle in the following references:
        //implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'


        // determine whether there is data, taken out if there is a direct
        // use SaveInstanceState do not need to use the following
        /*
        [1]: old-fashioned way
        myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
        if(savedInstanceState != null){
            myViewModel.getNumber().setValue(savedInstanceState.getInt(KEY_NUMBER));
        }
         */


        // New Method [2]: directly the phrase, it is determined that there is no need not empty
        // need to reference the build.gradle the implementation 'androidx.lifecycle: lifecycle-viewmodel-savedstate: 1.0.0-alpha01'
        Note // create ViewModel object when to take one argument: new SavedStateVMFactory (this)
        myViewModel = ViewModelProviders.of(this,new SavedStateVMFactory(this)).get(MyViewModel.class);

        binding.setData(myViewModel);
        binding.setLifecycleOwner(this);
    }

    // Save the data together
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_NUMBER,myViewModel.getNumber().getValue());
    }
}

  

ViewModel class

package com.example.viewmodelrestore;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;

MyViewModel the extends class ViewModel {public 
// [old] method private MutableLiveData<Integer> number;
//【new method】 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); } }

  

 

Guess you like

Origin www.cnblogs.com/gfwei/p/11780236.html