Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication.ui.main
- import androidx.lifecycle.ViewModelProvider
- import android.os.Bundle
- import android.util.Log
- import androidx.fragment.app.Fragment
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import androidx.lifecycle.ViewModel
- import androidx.lifecycle.lifecycleScope
- import androidx.lifecycle.viewModelScope
- import com.example.myapplication.R
- import kotlinx.coroutines.channels.Channel
- import kotlinx.coroutines.flow.*
- import kotlinx.coroutines.launch
- class MainFragment : Fragment() {
- companion object {
- fun newInstance() = MainFragment()
- }
- private lateinit var viewModel: MainViewModel
- override fun onCreateView(
- inflater: LayoutInflater, container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View {
- return inflater.inflate(R.layout.main_fragment, container, false)
- }
- override fun onActivityCreated(savedInstanceState: Bundle?) {
- super.onActivityCreated(savedInstanceState)
- viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
- viewModel.createEvent("onActivityCreated")
- viewLifecycleOwner.lifecycleScope.launchWhenResumed { // similar issue happens with launchWhenStarted
- viewModel.events
- .onStart {
- val state = lifecycle.currentState
- Log.d("TESTING", "Flow observer - Starting in state $state")
- }
- .onCompletion {
- val state = lifecycle.currentState
- Log.d("TESTING", "Flow observer - Completing in state $state")
- }
- .onEach {
- val state = lifecycle.currentState
- Log.d("TESTING", "Flow observer - Got value $it in state $state")
- }
- .catch {
- val state = lifecycle.currentState
- Log.d("TESTING", "Flow observer - caught $it")
- }
- .collect()
- }
- }
- override fun onStart() {
- super.onStart()
- viewModel.createEvent("onStart") // force a flow emission during this lifecycle event
- }
- override fun onResume() {
- super.onResume()
- viewModel.createEvent("onResume") // force a flow emission during this lifecycle event
- }
- override fun onPause() {
- super.onPause()
- viewModel.createEvent("onPause") // force a flow emission during this lifecycle event
- }
- override fun onStop() {
- super.onStop()
- viewModel.createEvent("onStop") // force a flow emission during this lifecycle event
- }
- override fun onDestroyView() {
- super.onDestroyView()
- viewModel.createEvent("onDestroyView") // force a flow emission during this lifecycle event
- }
- override fun onDestroy() {
- super.onDestroy()
- viewModel.createEvent("onDestroy") // force a flow emission during this lifecycle event
- }
- }
- class MainViewModel : ViewModel() {
- fun createEvent(eventName: String) {
- viewModelScope.launch {
- Log.d("TESTING", "MainViewModel createEvent() $eventName")
- _eventChannel.send(eventName)
- }
- }
- private val _eventChannel = Channel<String>(Channel.BUFFERED)
- val events = _eventChannel.receiveAsFlow()
- }
- D/TESTING: MainViewModel createEvent() onActivityCreated
- D/TESTING: MainViewModel createEvent() onStart
- D/TESTING: MainViewModel createEvent() onResume
- D/TESTING: Flow observer - Starting in state RESUMED
- D/TESTING: Flow observer - Got value onActivityCreated in state RESUMED
- D/TESTING: Flow observer - Got value onStart in state RESUMED
- D/TESTING: Flow observer - Got value onResume in state RESUMED
- <config change>
- D/TESTING: MainViewModel createEvent() onPause <------------- NOTE THIS is missing from the observer
- D/TESTING: MainViewModel createEvent() onStop
- D/TESTING: Flow observer - Completing in state CREATED
- D/TESTING: MainViewModel createEvent() onDestroyView
- D/TESTING: MainViewModel createEvent() onDestroy
- D/TESTING: MainViewModel createEvent() onActivityCreated
- D/TESTING: MainViewModel createEvent() onStart
- D/TESTING: MainViewModel createEvent() onResume
- D/TESTING: Flow observer - Starting in state RESUMED
- D/TESTING: Flow observer - Got value onStop in state RESUMED
- D/TESTING: Flow observer - Got value onDestroyView in state RESUMED
- D/TESTING: Flow observer - Got value onDestroy in state RESUMED
- D/TESTING: Flow observer - Got value onActivityCreated in state RESUMED
- D/TESTING: Flow observer - Got value onStart in state RESUMED
- D/TESTING: Flow observer - Got value onResume in state RESUMED
Advertisement
Add Comment
Please, Sign In to add comment