【Android】ViewModel和AndroidViewModel区别

在 Android 开发中,AndroidViewModel 和 ViewModel 都是 ViewModel 架构组件的一部分,用于在界面(如 Activity 或 Fragment)和数据之间进行分离,以保持数据在配置更改(如屏幕旋转)时的持久性。不过,它们之间存在一些关键区别。

继承关系与构造函数

  • ViewModel :ViewModel 是一个抽象类,位于 androidx.lifecycle 包中。它是所有 ViewModel 类的基类,构造函数较为简单,不包含任何特定的 Android 上下文依赖。示例如下:
java 复制代码
import androidx.lifecycle.ViewModel; 
public class MyViewModel extends ViewModel { 
// 构造函数无特殊要求 
    public MyViewModel() { 
    // 初始化操作 
    } 
}
  • AndroidViewModel :AndroidViewModel 继承自 ViewModel,并且在构造函数中接收 Application 上下文。这意味着它可以访问应用级别的资源,例如 Application 类的实例。示例如下:
js 复制代码
import android.app.Application; 
import androidx.lifecycle.AndroidViewModel; 
public class MyAndroidViewModel extends AndroidViewModel { 
    public MyAndroidViewModel(Application application) { 
    super(application); 
    // 可以使用 application 实例进行操作 
    } 
}
-   **ViewModel**:当 `ViewModel` 不需要访问 Android 上下文或只需要一些与上下文无关的数据和逻辑时,推荐使用 `ViewModel`。例如,处理简单的业务逻辑、数据转换等。

上下文的使用

  • ViewModel :由于 ViewModel 不持有任何 Android 上下文,所以它不适合直接访问 Android 特定的资源,如 Context、SharedPreferences 等。如果需要在 ViewModel 中执行与上下文相关的操作,应该通过依赖注入的方式将所需的资源传递给 ViewModel。
  • AndroidViewModel :AndroidViewModel 持有 Application 上下文,这使得它可以安全地访问应用级别的资源,例如 Application 类的实例、SharedPreferences 等。不过,需要注意的是,不应该在 AndroidViewModel 中持有 Activity、Fragment 或 View 的引用,因为这可能会导致内存泄漏。

使用场景

  • ViewModel :当 ViewModel 不需要访问 Android 上下文或只需要一些与上下文无关的数据和逻辑时,推荐使用 ViewModel。例如,处理简单的业务逻辑、数据转换等。
java 复制代码
import androidx.lifecycle.LiveData; 
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; 
public class CounterViewModel extends ViewModel { 
    private MutableLiveData<Integer> count = new MutableLiveData<>(0); 
    public LiveData<Integer> getCount() { 
        return count; 
    } 
    public void increment() { 
        count.setValue(count.getValue() + 1); 
    } 
}
  • AndroidViewModel :当 ViewModel 需要访问应用级别的资源时,应该使用 AndroidViewModel。例如,访问 SharedPreferences、数据库等。
typescript 复制代码
import android.app.Application; 
import androidx.lifecycle.AndroidViewModel; 
import androidx.lifecycle.LiveData; 
import androidx.lifecycle.MutableLiveData; 
import android.content.SharedPreferences; 
public class SettingsViewModel extends AndroidViewModel { 
    private SharedPreferences sharedPreferences; 
    private MutableLiveData<String> theme = new MutableLiveData<>(); 
    public SettingsViewModel(Application application) { 
    super(application); 
    sharedPreferences = application.getSharedPreferences("settings", 0); theme.setValue(sharedPreferences.getString("theme", "light")); } 
    
    public LiveData<String> getTheme() { 
        return theme; 
    } 
    
    public void setTheme(String theme) { 
        this.theme.setValue(theme)
        sharedPreferences.edit().putString("theme", theme).apply(); 
    } 
}

综上所述,ViewModel 适用于不需要 Android 上下文的场景,而 AndroidViewModel 适用于需要访问应用级资源的场景。在选择使用哪个类时,应根据具体的需求来决定。

相关推荐
FungLeo1 小时前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks2 小时前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui10 小时前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白1 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android1 天前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m1 天前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui1 天前
没人认领 .md:安卓把报告交给了音乐播放器打开
android
用户60709759149061 天前
AOSP 14 OTA 升级指导(四):update_engine 内部机制——Action 流水线与写盘
android
恋猫de小郭2 天前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
JMchen1232 天前
【Android 性能优化实战 60 讲】12 异步 Inflate 实战与痛击:源码拆解原生瓶颈,手写增强版布局预加载框架
android·性能优化·实战·启动优化·异步inflate·x2c