imäºÅ
lifacycle-viewModel-compoundâ®
https://www.youtube.com/watch?v=961Bd4UQOl0&list=PLgAI_5b_7BdRaBPAD5RMrzjoSwefDXpkw&index=9
Åè
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'
应用程序build.gradleâ«androidxâ®生命周期â
ViewModel-Composeâ2.5.15.1
ViewModelâ®
屏幕/ edit/â«editnoteviewModel.ktâ€ä½æ
viewModelâ€â€ ¼美
屏幕 - react - feactâ©â§contongemontountouncon
屏幕/±§ ç¾Å—°âch»é¢¢ editnotescreen.kt‥â£ã
†ViewModel‽æâ€
Angular Dartâ€1â原子 纽扣。{html,js,css}§ -
editnoteviewmodel.kt‽â€â
package com.example.hellojetpack.screen.edit
class EditNoteViewModel {
}
-
ViewModel⮯ã
EditnoteViewModelâ©â½½。
https://youtu.be/961Bd4UQOl0?t=87
import android.app.Application
import androidx.lifecycle.AndroidViewModel
class EditNoteViewModel(app:Application): AndroidViewModel(app){
}
androidViewModelâ€â
Androidâ®ViewModelâ€â©â€âżµã
§
应用Åâ®应用程序
â®应用程序 all uphouthouthoutouthoutoutoutout
EditnoteViewModelâ©©ââ€â€½
class EditNoteViewModel(
app:Application
): AndroidViewModel(app){
val repo: NoteRepository = DatastoreNoteRepository(app)
}
†®
存储库â®®stry
té -ulapâul ap and opera enera enderaâ€:00'p> od od fool
ComposableâViewModel‼¶
editnotescreenâ®Å¼â€� ©ââ€â€ -
https://youtu.be/961Bd4UQOl0?t=136
@Composable
fun EditScreen(viewModel: EditNoteViewModel = viewModel()
) {
// ...
}
组件çstary/edit/editnotescreen.kt
ââ€â€â©ä½美 -
viewModelâ§Å软¶ â®®
editscreené¢æ°°¼¼
â©â€â€âviewmodel()‮® €€
editnoteviewModelâ€â€Å® -
EditnoteviewModelâ€â€â©â€â€â€ªstry
应用程序â}
应用程序迷, ªedit edit >
@Composable
fun NoteApp() {
EditScreen()
}
editnotescreenâ�组件ç稿 noteapp - c c
MainActivity(root)
- > noteapp(容器)
- > editnotescreen(组件)
- > editnoteviewModel(viewModel)
â€
editnotescreenâ®onClickâ§eitnoteViewModelâ®保存
fun EditScreen(viewModel: EditNoteViewModel = viewModel() ) {
var body by remember { mutableStateOf("") }
Scaffold(
topBar = {
TopAppBar(
title = {
Text(text = "NoteApp")
},
actions = {
IconButton(onClick = {
println(body)
viewModel.save(body)
}) {
Icon(
Icons.Default.Done,
contentDescription = stringResource(id = R.string.app_name)
)
}
}
)
}
)
// ...
脚手架> topappbar>动作> iconbutton>
â£ã
viewModel.Save(Body)‥â€â
身体 -