当前位置:   article > 正文

Android jetpack viewModel 屏幕旋转数据如何保存。_jetpack+旋转后保持数据

jetpack+旋转后保持数据

解决的问题

瞬态数据的丢失。

异步调用的内存泄露

类膨胀提高维护难度和测试难度。

参考:2021年最全面的Jetpack系统学习课程,看他就够了,更新中_哔哩哔哩_bilibili

布局

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".activity.ViewModelActivity">
  8. <TextView
  9. android:id="@+id/textView"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="0"
  13. app:layout_constraintBottom_toBottomOf="parent"
  14. app:layout_constraintEnd_toEndOf="parent"
  15. app:layout_constraintHorizontal_bias="0.498"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent"
  18. app:layout_constraintVertical_bias="0.396" />
  19. <Button
  20. android:id="@+id/button3"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="加1"
  24. android:onClick="addOne"
  25. app:layout_constraintBottom_toBottomOf="parent"
  26. app:layout_constraintEnd_toEndOf="parent"
  27. app:layout_constraintHorizontal_bias="0.498"
  28. app:layout_constraintStart_toStartOf="parent"
  29. app:layout_constraintTop_toBottomOf="@+id/textView"
  30. app:layout_constraintVertical_bias="0.395" />
  31. </androidx.constraintlayout.widget.ConstraintLayout>

首先我们知道。在旋转屏幕的时候如果不重写他的onSaveInstace的方法。一些状态将无法被保存

也就是会被清空掉

  1. package com.anguomob.jecpack.activity
  2. import androidx.appcompat.app.AppCompatActivity
  3. import android.os.Bundle
  4. import android.view.View
  5. import androidx.lifecycle.ViewModelProvider
  6. import com.anguomob.jecpack.R
  7. import com.anguomob.jecpack.databinding.ActivityMainBinding
  8. import com.anguomob.jecpack.databinding.ActivityViewModelBinding
  9. import com.anguomob.jecpack.viewmodel.MyViewModel
  10. class ViewModelActivity : AppCompatActivity() {
  11. lateinit var binding: ActivityViewModelBinding
  12. var number = 0;
  13. override fun onCreate(savedInstanceState: Bundle?) {
  14. super.onCreate(savedInstanceState)
  15. binding = ActivityViewModelBinding.inflate(layoutInflater)
  16. setContentView(binding.root)
  17. binding.textView.text = number.toString()
  18. }
  19. fun addOne(view: View) {
  20. binding.textView.text = (++number).toString()
  21. }
  22. }

这种数据是在旋转后会变为0的

如果使用viewModel则不会

  1. package com.anguomob.jecpack.activity
  2. import androidx.appcompat.app.AppCompatActivity
  3. import android.os.Bundle
  4. import android.view.View
  5. import androidx.lifecycle.ViewModelProvider
  6. import com.anguomob.jecpack.R
  7. import com.anguomob.jecpack.databinding.ActivityMainBinding
  8. import com.anguomob.jecpack.databinding.ActivityViewModelBinding
  9. import com.anguomob.jecpack.viewmodel.MyViewModel
  10. class ViewModelActivity : AppCompatActivity() {
  11. lateinit var binding: ActivityViewModelBinding
  12. lateinit var viewModel: MyViewModel
  13. override fun onCreate(savedInstanceState: Bundle?) {
  14. super.onCreate(savedInstanceState)
  15. binding = ActivityViewModelBinding.inflate(layoutInflater)
  16. setContentView(binding.root)
  17. viewModel = ViewModelProvider(
  18. this,
  19. ViewModelProvider.AndroidViewModelFactory.getInstance(application)
  20. ).get(
  21. MyViewModel::class.java
  22. )
  23. binding.textView.text = viewModel.number.toString()
  24. }
  25. fun addOne(view: View) {
  26. binding.textView.text = (++viewModel.number).toString()
  27. }
  28. }
MyViewModel的内容也很简单
  1. package com.anguomob.jecpack.viewmodel
  2. import androidx.lifecycle.ViewModel
  3. class MyViewModel : ViewModel() {
  4. var number = 0
  5. }

这个原理得亏与viewModel的声明周期比较长

所以不要往viewModel里面传入Context 会造成内存泄露

如果要用Context 请使用 AndroidViewModel中的Application

 什么意思呢

修改下我们刚才的代码

  1. package com.anguomob.jecpack.viewmodel
  2. import android.app.Application
  3. import androidx.lifecycle.AndroidViewModel
  4. import androidx.lifecycle.ViewModel
  5. class MyViewModel(application: Application) : AndroidViewModel(application) {
  6. var number = 0
  7. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/627527
推荐阅读
相关标签
  

闽ICP备14008679号