Guest User

Untitled

a guest
Oct 27th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.78 KB | None | 0 0
  1. package com.example.myapplication.ui.main
  2.  
  3. import androidx.lifecycle.ViewModelProvider
  4. import android.os.Bundle
  5. import android.util.Log
  6. import androidx.fragment.app.Fragment
  7. import android.view.LayoutInflater
  8. import android.view.View
  9. import android.view.ViewGroup
  10. import androidx.lifecycle.ViewModel
  11. import androidx.lifecycle.lifecycleScope
  12. import androidx.lifecycle.viewModelScope
  13. import com.example.myapplication.R
  14. import kotlinx.coroutines.channels.Channel
  15. import kotlinx.coroutines.flow.*
  16. import kotlinx.coroutines.launch
  17.  
  18. class MainFragment : Fragment() {
  19.  
  20.     companion object {
  21.         fun newInstance() = MainFragment()
  22.     }
  23.  
  24.     private lateinit var viewModel: MainViewModel
  25.  
  26.     override fun onCreateView(
  27.         inflater: LayoutInflater, container: ViewGroup?,
  28.         savedInstanceState: Bundle?
  29.     ): View {
  30.         return inflater.inflate(R.layout.main_fragment, container, false)
  31.     }
  32.  
  33.     override fun onActivityCreated(savedInstanceState: Bundle?) {
  34.         super.onActivityCreated(savedInstanceState)
  35.         viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
  36.         viewModel.createEvent("onActivityCreated")
  37.  
  38.         viewLifecycleOwner.lifecycleScope.launchWhenResumed { // similar issue happens with launchWhenStarted
  39.             viewModel.events
  40.                     .onStart {
  41.                         val state = lifecycle.currentState
  42.                         Log.d("TESTING", "Flow observer - Starting in state $state")
  43.                     }
  44.                     .onCompletion {
  45.                         val state = lifecycle.currentState
  46.                         Log.d("TESTING", "Flow observer - Completing in state $state")
  47.                     }
  48.                     .onEach {
  49.                         val state = lifecycle.currentState
  50.                         Log.d("TESTING", "Flow observer - Got value $it in state $state")
  51.                     }
  52.                     .catch {
  53.                         val state = lifecycle.currentState
  54.                         Log.d("TESTING", "Flow observer - caught $it")
  55.                     }
  56.                     .collect()
  57.         }
  58.     }
  59.  
  60.     override fun onStart() {
  61.         super.onStart()
  62.         viewModel.createEvent("onStart") // force a flow emission during this lifecycle event
  63.     }
  64.  
  65.     override fun onResume() {
  66.         super.onResume()
  67.         viewModel.createEvent("onResume") // force a flow emission during this lifecycle event
  68.     }
  69.  
  70.     override fun onPause() {
  71.         super.onPause()
  72.         viewModel.createEvent("onPause") // force a flow emission during this lifecycle event
  73.     }
  74.  
  75.     override fun onStop() {
  76.         super.onStop()
  77.         viewModel.createEvent("onStop") // force a flow emission during this lifecycle event
  78.     }
  79.  
  80.     override fun onDestroyView() {
  81.         super.onDestroyView()
  82.         viewModel.createEvent("onDestroyView") // force a flow emission during this lifecycle event
  83.     }
  84.  
  85.     override fun onDestroy() {
  86.         super.onDestroy()
  87.         viewModel.createEvent("onDestroy") // force a flow emission during this lifecycle event
  88.     }
  89. }
  90.  
  91. class MainViewModel : ViewModel() {
  92.     fun createEvent(eventName: String) {
  93.         viewModelScope.launch {
  94.         Log.d("TESTING", "MainViewModel createEvent() $eventName")
  95.             _eventChannel.send(eventName)
  96.         }
  97.     }
  98.  
  99.     private val _eventChannel = Channel<String>(Channel.BUFFERED)
  100.     val events = _eventChannel.receiveAsFlow()
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  D/TESTING: MainViewModel createEvent() onActivityCreated
  110.  D/TESTING: MainViewModel createEvent() onStart
  111.  D/TESTING: MainViewModel createEvent() onResume
  112.  D/TESTING: Flow observer - Starting in state RESUMED
  113.  D/TESTING: Flow observer - Got value onActivityCreated in state RESUMED
  114.  D/TESTING: Flow observer - Got value onStart in state RESUMED
  115.  D/TESTING: Flow observer - Got value onResume in state RESUMED
  116.  
  117. <config change>
  118.  
  119.  D/TESTING: MainViewModel createEvent() onPause <------------- NOTE THIS is missing from the observer
  120.  D/TESTING: MainViewModel createEvent() onStop
  121.  D/TESTING: Flow observer - Completing in state CREATED
  122.  D/TESTING: MainViewModel createEvent() onDestroyView
  123.  D/TESTING: MainViewModel createEvent() onDestroy
  124.  D/TESTING: MainViewModel createEvent() onActivityCreated
  125.  D/TESTING: MainViewModel createEvent() onStart
  126.  D/TESTING: MainViewModel createEvent() onResume
  127.  D/TESTING: Flow observer - Starting in state RESUMED
  128.  D/TESTING: Flow observer - Got value onStop in state RESUMED
  129.  D/TESTING: Flow observer - Got value onDestroyView in state RESUMED
  130.  D/TESTING: Flow observer - Got value onDestroy in state RESUMED
  131.  D/TESTING: Flow observer - Got value onActivityCreated in state RESUMED
  132.  D/TESTING: Flow observer - Got value onStart in state RESUMED
  133.  D/TESTING: Flow observer - Got value onResume in state RESUMED
Advertisement
Add Comment
Please, Sign In to add comment