最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Jetpack組件之ViewModel使用詳解

 更新時間:2023年04月03日 08:38:28   作者:別偷我的豬_09  
Android中的ViewModel是一個可以用來存儲UI相關的數(shù)據(jù)的類。ViewModel的生命周期會比創(chuàng)建它的Activity、Fragment的生命周期長,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧

ViewModel的誕生

解決以下幾個問題:

  • 瞬態(tài)數(shù)據(jù)丟失
  • 異步調(diào)用的內(nèi)存泄漏

當我們?nèi)∫援惒讲僮鲄^(qū)網(wǎng)絡請求時,而我們又在網(wǎng)絡數(shù)據(jù)返回前點擊了“返回按鈕”,此時Activity 已經(jīng)銷毀了,但是網(wǎng)絡請求的這個對象還在請求,且一直占據(jù)在內(nèi)存里面。而 Activity 已經(jīng)銷毀了,就在也拿不到 請求網(wǎng)絡的這個對象了,這就是內(nèi)存泄漏了。

內(nèi)存泄漏:一個對象我們已經(jīng)引用(使用)不到它了,但它又占有著內(nèi)存。GC 以為該對象還能夠使用,就沒有回收它。當這種情況過多,內(nèi)存就會不斷被占用,就會導致手機卡頓。

  • 類膨脹提高維護難度和測試難度

在 Activity 里面寫的代碼越來越多,導致維護難度和測試難度不斷提高。

ViewModel的作用

  • 它是介于 View(視圖)和 Model(數(shù)據(jù)模型)之間的橋梁
  • 使視圖和數(shù)據(jù)能夠分離也能保持通信

ViewModel的簡單應用

屏幕旋轉(zhuǎn)后用戶操作的數(shù)據(jù)仍然存在,即保存瞬時態(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,里面有一個數(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));
    }
    /**
     * 點擊 button 時,textView 的內(nèi)容隨著 viewModel.number 的改變而改變
     */
    public void plusNumber(View view) {
        textView.setText(String.valueOf(++viewModel.number));
    }
}

ViewModel 的生命周期獨立于配置變化,不管 Activity 到了生命周期的那個階段,ViewModel 上的數(shù)據(jù)它都存在且可以訪問,即便 Activity 銷毀了 ViewModel 上的數(shù)據(jù)仍然存在。如下圖:

注意:

不要向 ViewModel 里傳入 Context,會導致內(nèi)存泄漏。如果要使用 Contetxt,那就使用 AndroidViewModel 中的 Application 來代替 Context。

public class MyViewModel extends AndroidViewModel

到此這篇關于Android Jetpack組件之ViewModel使用詳解的文章就介紹到這了,更多相關Android ViewModel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Android實現(xiàn)水波紋擴散效果

    Android實現(xiàn)水波紋擴散效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)水波紋擴散效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Android okhttp的啟動流程及源碼解析

    Android okhttp的啟動流程及源碼解析

    這篇文章主要介紹了Android okhttp的啟動流程及源碼解析,幫助大家更好的理解和學習使用Android開發(fā),感興趣的朋友可以了解下
    2021-03-03
  • 解決Android模擬器端口被占用問題的辦法

    解決Android模擬器端口被占用問題的辦法

    這篇文章主要為大家分享了Android模擬器端口被占用問題的解決辦法,遇到Android模擬器端口被占用的時候,真的很頭疼,如何才能解決端口被占用的問題,下文為大家揭曉
    2015-12-12
  • Android不規(guī)則封閉區(qū)域填充色彩的實例代碼

    Android不規(guī)則封閉區(qū)域填充色彩的實例代碼

    這篇文章主要介紹了Android不規(guī)則封閉區(qū)域填充色彩的實例代碼, 具有很好的參考價值,希望對大家有所幫助,一起跟隨小編過來看看吧
    2018-05-05
  • Android集成微信支付功能

    Android集成微信支付功能

    這篇文章主要為大家詳細介紹了Android集成微信支付功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android實現(xiàn)漸變圓環(huán)、圓形進度條效果

    Android實現(xiàn)漸變圓環(huán)、圓形進度條效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)漸變圓環(huán)、圓形進度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android EditText每4位自動添加空格效果

    Android EditText每4位自動添加空格效果

    這篇文章主要給大家介紹了關于Android EditText每4位自動添加空格效果的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用EditText具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • Repo工作原理和使用介紹

    Repo工作原理和使用介紹

    Repo是谷歌用Python腳本寫的調(diào)用git的一個腳本,可以實現(xiàn)管理多個git庫。本文詳細講解了Repo的工作原理和使用介紹,需要的朋友可以收藏下,方便下次瀏覽觀看
    2021-12-12
  • Android EditText實現(xiàn)分割輸入內(nèi)容

    Android EditText實現(xiàn)分割輸入內(nèi)容

    這篇文章主要為大家詳細介紹了Android EditText實現(xiàn)分割輸入內(nèi)容的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Android自定義仿ios加載彈窗

    Android自定義仿ios加載彈窗

    這篇文章主要為大家詳細介紹了Android自定義仿ios加載彈窗,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05

最新評論

青海省| 中江县| 石门县| 张家界市| 通辽市| 利津县| 喜德县| 华蓥市| 苍溪县| 正安县| 长阳| 山阴县| 香港 | 乌兰县| 桦南县| 赣榆县| 慈溪市| 大宁县| 布尔津县| 顺平县| 清原| 永登县| 百色市| 万安县| 新巴尔虎右旗| 启东市| 阜新市| 双峰县| 股票| 称多县| 抚州市| 玛多县| 宁国市| 新竹市| 靖江市| 汉阴县| 泰宁县| 鹤山市| 昌江| 木兰县| 长葛市|