赞
踩
解决的问题
瞬态数据的丢失。
异步调用的内存泄露
类膨胀提高维护难度和测试难度。
参考:2021年最全面的Jetpack系统学习课程,看他就够了,更新中_哔哩哔哩_bilibili
布局
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout 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"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".activity.ViewModelActivity">
-
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="0"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.498"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.396" />
-
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="加1"
- android:onClick="addOne"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.498"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/textView"
- app:layout_constraintVertical_bias="0.395" />
- </androidx.constraintlayout.widget.ConstraintLayout>
首先我们知道。在旋转屏幕的时候如果不重写他的onSaveInstace的方法。一些状态将无法被保存
也就是会被清空掉
- package com.anguomob.jecpack.activity
-
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.view.View
- import androidx.lifecycle.ViewModelProvider
- import com.anguomob.jecpack.R
- import com.anguomob.jecpack.databinding.ActivityMainBinding
- import com.anguomob.jecpack.databinding.ActivityViewModelBinding
- import com.anguomob.jecpack.viewmodel.MyViewModel
-
- class ViewModelActivity : AppCompatActivity() {
- lateinit var binding: ActivityViewModelBinding
-
- var number = 0;
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- binding = ActivityViewModelBinding.inflate(layoutInflater)
- setContentView(binding.root)
-
-
- binding.textView.text = number.toString()
-
- }
-
- fun addOne(view: View) {
- binding.textView.text = (++number).toString()
- }
- }
这种数据是在旋转后会变为0的
如果使用viewModel则不会
- package com.anguomob.jecpack.activity
-
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.view.View
- import androidx.lifecycle.ViewModelProvider
- import com.anguomob.jecpack.R
- import com.anguomob.jecpack.databinding.ActivityMainBinding
- import com.anguomob.jecpack.databinding.ActivityViewModelBinding
- import com.anguomob.jecpack.viewmodel.MyViewModel
-
- class ViewModelActivity : AppCompatActivity() {
- lateinit var binding: ActivityViewModelBinding
- lateinit var viewModel: MyViewModel
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- binding = ActivityViewModelBinding.inflate(layoutInflater)
- setContentView(binding.root)
-
- viewModel = ViewModelProvider(
- this,
- ViewModelProvider.AndroidViewModelFactory.getInstance(application)
- ).get(
- MyViewModel::class.java
- )
- binding.textView.text = viewModel.number.toString()
-
- }
-
- fun addOne(view: View) {
- binding.textView.text = (++viewModel.number).toString()
- }
- }
MyViewModel的内容也很简单
- package com.anguomob.jecpack.viewmodel
-
- import androidx.lifecycle.ViewModel
-
- class MyViewModel : ViewModel() {
- var number = 0
-
- }
这个原理得亏与viewModel的声明周期比较长
所以不要往viewModel里面传入Context 会造成内存泄露
如果要用Context 请使用 AndroidViewModel中的Application
什么意思呢
修改下我们刚才的代码
- package com.anguomob.jecpack.viewmodel
-
- import android.app.Application
- import androidx.lifecycle.AndroidViewModel
- import androidx.lifecycle.ViewModel
- class MyViewModel(application: Application) : AndroidViewModel(application) {
- var number = 0
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。