Android Jetpack組件之ViewModel使用詳解
ViewModel的誕生
解決以下幾個(gè)問題:
- 瞬態(tài)數(shù)據(jù)丟失
- 異步調(diào)用的內(nèi)存泄漏
當(dāng)我們?nèi)∫援惒讲僮鲄^(qū)網(wǎng)絡(luò)請求時(shí),而我們又在網(wǎng)絡(luò)數(shù)據(jù)返回前點(diǎn)擊了“返回按鈕”,此時(shí)Activity 已經(jīng)銷毀了,但是網(wǎng)絡(luò)請求的這個(gè)對象還在請求,且一直占據(jù)在內(nèi)存里面。而 Activity 已經(jīng)銷毀了,就在也拿不到 請求網(wǎng)絡(luò)的這個(gè)對象了,這就是內(nèi)存泄漏了。
內(nèi)存泄漏:一個(gè)對象我們已經(jīng)引用(使用)不到它了,但它又占有著內(nèi)存。GC 以為該對象還能夠使用,就沒有回收它。當(dāng)這種情況過多,內(nèi)存就會(huì)不斷被占用,就會(huì)導(dǎo)致手機(jī)卡頓。

- 類膨脹提高維護(hù)難度和測試難度
在 Activity 里面寫的代碼越來越多,導(dǎo)致維護(hù)難度和測試難度不斷提高。
ViewModel的作用
- 它是介于 View(視圖)和 Model(數(shù)據(jù)模型)之間的橋梁
- 使視圖和數(shù)據(jù)能夠分離也能保持通信
ViewModel的簡單應(yīng)用
屏幕旋轉(zhuǎn)后用戶操作的數(shù)據(jù)仍然存在,即保存瞬時(shí)態(tài)數(shù)據(jù)。

1. activity_main.xml
<?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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.247" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="plusNumber"/>
</androidx.constraintlayout.widget.ConstraintLayout>2. MyViewModel.java
MyViewModel 繼承自 ViewModle,里面有一個(gè)數(shù)據(jù)變量 number。
package com.example.viewmodel;
import androidx.lifecycle.ViewModel;
public class MyViewModel extends ViewModel {
public int number;
}3. MainActivity.java
通過 new ViewModelProvider.get() 方法來獲取到自定義的 MyViewModel 對象。
package com.example.viewmodel;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private MyViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
// 獲取到 MyViewModel
viewModel = new ViewModelProvider(this,
new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyViewModel.class);
textView.setText(String.valueOf(viewModel.number));
}
/**
* 點(diǎn)擊 button 時(shí),textView 的內(nèi)容隨著 viewModel.number 的改變而改變
*/
public void plusNumber(View view) {
textView.setText(String.valueOf(++viewModel.number));
}
}ViewModel 的生命周期獨(dú)立于配置變化,不管 Activity 到了生命周期的那個(gè)階段,ViewModel 上的數(shù)據(jù)它都存在且可以訪問,即便 Activity 銷毀了 ViewModel 上的數(shù)據(jù)仍然存在。如下圖:

注意:
不要向 ViewModel 里傳入 Context,會(huì)導(dǎo)致內(nèi)存泄漏。如果要使用 Contetxt,那就使用 AndroidViewModel 中的 Application 來代替 Context。
public class MyViewModel extends AndroidViewModel
到此這篇關(guān)于Android Jetpack組件之ViewModel使用詳解的文章就介紹到這了,更多相關(guān)Android ViewModel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android學(xué)習(xí)筆記——Menu介紹(一)
Android3.0(API level 11)開始,Android設(shè)備不再需要專門的菜單鍵。隨著這種變化,Android app應(yīng)該取消對傳統(tǒng)6項(xiàng)菜單的依賴。取而代之的是提供anction bar來提供基本的用戶功能2014-10-10
淺談關(guān)于Android WebView上傳文件的解決方案
這篇文章主要介紹了淺談關(guān)于Android WebView上傳文件的解決方案 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android之ArcSlidingHelper制作圓弧滑動(dòng)效果
這篇文章主要介紹了Android之ArcSlidingHelper制作圓弧滑動(dòng)效果,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Android項(xiàng)目中引入aar包的正確方法介紹
生成aar之后下一步就是如何引用本地的aar文件,下面這篇文章主要給大家介紹了關(guān)于Android項(xiàng)目中引入aar包的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Android編程簡易實(shí)現(xiàn)XML解析的方法詳解
這篇文章主要介紹了Android編程簡易實(shí)現(xiàn)XML解析的方法,結(jié)合實(shí)例形式總結(jié)分析了Android操作xml文件的各種常見技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
Android 中動(dòng)態(tài)加載.jar的實(shí)現(xiàn)步驟
本文介紹動(dòng)態(tài)加載 .jar的實(shí)現(xiàn)步驟,這將對你的android開發(fā)很有幫助,剛興趣的朋友可以了解下哦2013-01-01
Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)功能
這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
調(diào)用startService會(huì)拋出IllegalStateException異常解決
這篇文章主要為大家介紹了調(diào)用startService會(huì)拋出IllegalStateException異常解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

