SavedStated
作者:互联网
通过之前的努力,引用viewmodel而解决了屏幕翻转,语言切换而导致的数据丢失
现在引入新的模式ViewModelSavedState来使得Activity重新创建时UI数据也不会丢失
首先是Activity中的导入viewmodel改变
myViewModel=new ViewModelProvider(this,new SavedStateViewModelFactory(getApplication(),this)).get(MyViewModel.class);
需要导入两个依赖
implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
其次MyViewModel中改变也很多,直接上代码吧
package com.example.viewmodelrestore; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.SavedStateHandle; import androidx.lifecycle.ViewModel; public class MyViewModel extends ViewModel { private SavedStateHandle handle; public MyViewModel(SavedStateHandle handle){ this.handle= handle;//这里是当activity重新创建时,要把之前的数重新导入 } public MutableLiveData<Integer> getNumber() { if(!handle.contains(MainActivity.KEY_NUMBER)){ handle.set(MainActivity.KEY_NUMBER,0); } return handle.getLiveData(MainActivity.KEY_NUMBER); } public void add(){ getNumber().setValue(getNumber().getValue()+1); } }
MainActivity
package com.example.viewmodelrestore; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import androidx.lifecycle.SavedStateViewModelFactory; import androidx.lifecycle.ViewModelProvider; import android.os.Bundle; import com.example.viewmodelrestore.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { ActivityMainBinding binding; MyViewModel myViewModel; 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=new ViewModelProvider(this,new SavedStateViewModelFactory(getApplication(),this)).get(MyViewModel.class); binding.setData(myViewModel); binding.setLifecycleOwner(this); } }
activity_main
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="data" type="com.example.viewmodelrestore.MyViewModel" /> </data> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="175dp" android:layout_marginTop="162dp" android:layout_marginEnd="177dp" android:layout_marginBottom="118dp" android:text="@{String.valueOf(data.getNumber)}" app:layout_constraintBottom_toTopOf="@+id/button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button1" android:onClick="@{()->data.add()}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>
标签:SavedStated,handle,androidx,public,import,MyViewModel,lifecycle 来源: https://www.cnblogs.com/nian-nian/p/15771706.html