Android 的 MVI(Model-View-Intent) 是一种响应式架构模式,强调单向数据流和状态管理,适合复杂 UI 交互的应用。以下是其核心要点:
1. 核心组件
- Model (状态) :
用不可变数据类表示应用状态(如data class UiState
),确保状态变更可预测。 - View (UI) :
Activity/Fragment 只负责渲染状态和发送用户意图(Intent),不处理逻辑。 - Intent (用户意图) :
用户操作(如按钮点击)被封装为 Intent(如Sealed class UserIntent
),传递给 ViewModel 处理。
2. 数据流(单向)
- 用户触发 Intent →
View 将UserIntent
发送给 ViewModel。 - ViewModel 处理 →
根据 Intent 执行业务逻辑(如调用 Repository),生成新状态。 - 状态更新 →
ViewModel 将新状态UiState
推送给 View。 - UI 渲染 →
View 根据最新状态自动更新界面。
3. 关键特点
- 单一数据源:所有状态集中管理,避免数据不一致。
- 不可变状态:状态不可修改,只能通过创建新实例更新。
- 响应式编程:常用 Kotlin Flow 或 RxJava 实现数据流。
- 纯函数式处理:Intent 到状态的转换是纯函数,易于测试。
4. 代码示例
状态定义
kotlin
data class MainUiState(
val isLoading: Boolean = false,
val data: List<String> = emptyList(),
val error: String? = null
)
用户意图
kotlin
sealed class MainIntent {
object LoadData : MainIntent()
data class DeleteItem(val id: String) : MainIntent()
}
ViewModel 处理
kotlin
class MainViewModel : ViewModel() {
private val _state = MutableStateFlow(MainUiState())
val state: StateFlow<MainUiState> = _state.asStateFlow()
fun processIntent(intent: MainIntent) {
when (intent) {
is MainIntent.LoadData -> loadData()
is MainIntent.DeleteItem -> deleteItem(intent.id)
}
}
private fun loadData() {
viewModelScope.launch {
_state.update { it.copy(isLoading = true) }
repository.getData()
.onSuccess { _state.update { it.copy(data = it, isLoading = false) } }
.onFailure { _state.update { it.copy(error = "加载失败", isLoading = false) } }
}
}
}
View 绑定
kotlin
// Activity/Fragment 中观察状态
viewModel.state.collect { state ->
if (state.isLoading) showLoading()
state.data?.let { updateList(it) }
state.error?.let { showError(it) }
}
// 发送 Intent
button.setOnClickListener {
viewModel.processIntent(MainIntent.LoadData)
}
5. 对比其他架构
- MVP:MVI 用状态驱动 UI,避免 MVP 中 View 接口的冗余。
- MVVM:MVI 明确约束状态管理,MVVM 的 LiveData 更松散。
- Redux:类似单向数据流,但 MVI 更轻量,适合 Android 场景。
6. 优缺点
-
优点:
- 状态管理清晰,适合复杂 UI。
- 易于调试(状态变化可追溯)。
- 天然支持 Jetpack Compose。
-
缺点:
- 模板代码较多(需定义大量状态类)。
- 学习成本较高(需熟悉响应式编程)。
适用场景
- 需要严格状态管理的页面(如表单、多步骤操作)。
- 与 Jetpack Compose 结合时效果更佳。
通过 MVI,可以构建高可维护性、可测试性的 Android 应用。推荐结合 Kotlin Flow 和 Jetpack 组件(如 ViewModel、StateFlow)使用。