Функция инициализации ViewModel:
Код: Выделить всё
init{
viewModelScope.launch {
_eventFlow.emit(
UiEvent.TakePhoto
)
}
}
Код: Выделить всё
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED)
{
viewModel.eventFlow.collectLatest { event ->
when(event){
is PhotoViewModel.UiEvent.TakePhoto ->
{
takePhoto()
}
}
}
}
}
}
private fun takePhoto()
{
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
resultLauncher.launch(cameraIntent)
}
Код: Выделить всё
init{
viewModelScope.launch {
delay(1000)
_eventFlow.emit(
UiEvent.TakePhoto
)
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... nction-cor