title: Kotlin StateFlow multiple subscriptions after returning from another Fragment
body: <p>I'm trying to implement StateFlow in my app with MVVM architecture between ViewModel and Fragment.</p>
  <p><em>In ViewModel:</em></p>
  <pre><code>...
  
  private val _eventDataState = MutableStateFlow&lt;EventDataState&gt;(EventDataState.Loading)
  val eventDataState: StateFlow&lt;EventDataState&gt; = _eventDataState
  
  ...
  </code></pre>
  <p><em>In Fragment:</em></p>
  <pre><code>...
  
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          
          ...
  
          lifecycleScope.launch {
              repeatOnLifecycle(Lifecycle.State.STARTED) {
                  viewModel.eventDataState.collect { eventDataState -&gt;
                      when (eventDataState) {
                          is EventDataState.Loading -&gt; {}
                          is EventDataState.Success -&gt; onEventDataUpdated(data = eventDataState)
                          is EventDataState.Deleted -&gt; onEventDataDeleted()
                      }
                  }
              }
          }
          viewModel.getEventData()
      }
  
  ...
  </code></pre>
  <p>All seems to work well:</p>
  <pre><code>2022-04-15 17:57:20.352 5251-5251/ ... E/EVENT: Data state: Success(...)
  
  </code></pre>
  <p>But when I switch to another Fragment through Activity's SupportFragmentManager and then hit Back button I get multiple responses from StateFlow:</p>
  <pre><code>2022-04-15 17:57:30.358 5251-5251/ ... E/EVENT: Data state: Success(...)
  2022-04-15 17:57:30.362 5251-5251/ ... E/EVENT: Data state: Success(...)
  </code></pre>
  <p>So the more times I switch to another Fragment and back - the more copies of response I get from StateFlow (this is not initial StateFlow value).</p>
  <p>My guess is that when another Fragment are called the first one destroys its view but subscription to Flow is still exists, and when returning back from another Fragment the first one calls its onViewCreated and so creating another copy of subscription to this Flow. Nevertheless I was unable to find any solution to my case on stackoverflow or any other resources.</p>
  <p>What have I missed?</p>