I'm adding a new contact on the NewContactActivity and after complete inserts on the DB and closes the activity but this not update the contacts list
MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppTheme {
val contatoViewModel: ContatoViewModel = viewModel(factory = ContatoViewModelFactory(LocalContext.current.applicationContext as Application))
val contatos = contatoViewModel.readAllData.observeAsState(listOf()).value
val novoContatoLaunch = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
run {
when(it.resultCode) {
RESULT_OK -> {
Toast.makeText(this@MainActivity, "Contato Salvo!", Toast.LENGTH_LONG).show()
//TODO I need to update the contatos here
}
}
}
}
MainView(contatos) {
novoContatoLaunch.launch(Intent(this@MainActivity, NovoContatoActivity::class.java))
}
}
}
}
NewContactActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppTheme {
val contatoViewModel: ContatoViewModel = viewModel(factory = ContatoViewModelFactory(LocalContext.current.applicationContext as Application))
NovoContatoView {
contatoViewModel.addContato(it)
setResult(RESULT_OK)
finish()
}
}
}
}
Seems like the answer will be inside your ContatoViewModel
. If you're using LiveData
via room library this should update automatically after the change in database. If not, just call some method in ContatoViewModel
that will read changes from the database and update the underlying live data
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.