【Android】ViewModel和AndroidViewModel区别

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

继承关系与构造函数

  • ViewModelViewModel 是一个抽象类,位于 androidx.lifecycle 包中。它是所有 ViewModel 类的基类,构造函数较为简单,不包含任何特定的 Android 上下文依赖。示例如下:
java 复制代码
import androidx.lifecycle.ViewModel; 
public class MyViewModel extends ViewModel { 
// 构造函数无特殊要求 
    public MyViewModel() { 
    // 初始化操作 
    } 
}
  • AndroidViewModelAndroidViewModel 继承自 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 特定的资源,如 ContextSharedPreferences 等。如果需要在 ViewModel 中执行与上下文相关的操作,应该通过依赖注入的方式将所需的资源传递给 ViewModel
  • AndroidViewModelAndroidViewModel 持有 Application 上下文,这使得它可以安全地访问应用级别的资源,例如 Application 类的实例、SharedPreferences 等。不过,需要注意的是,不应该在 AndroidViewModel 中持有 ActivityFragmentView 的引用,因为这可能会导致内存泄漏。

使用场景

  • 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 适用于需要访问应用级资源的场景。在选择使用哪个类时,应根据具体的需求来决定。

相关推荐
阿巴斯甜16 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker17 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952718 小时前
Andorid Google 登录接入文档
android
黄林晴19 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android