title: Fragment popbackstack trigger lifecyclescope collect
body: <p><strong>Situation</strong></p>
  <p>I submit data <code>setTripDeliver</code>, the collect works fine (trigger LOADING and then SUCCESS). I pressed a button go to next fragment B (using <code>replace</code>). After that, I press back button (using <code>popbackstack</code>). the collect SUCCESS triggered.</p>
  <p><strong>Codes Related</strong></p>
  <p>These codes at the <code>FragmentA.kt</code> inside <code>onViewCreated</code>.</p>
  <pre><code>private fun startLifeCycle() {
      viewLifecycleOwner.lifecycleScope.launch {
          viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
              launch {
                  collectTripDeliver()
              }
              launch {
                  collectTripReattempt()
              }
          }
      }
  }
  </code></pre>
  <p>These codes when to submit data at a button <code>setOnClickListener</code>.</p>
  <pre><code>viewLifecycleOwner.lifecycleScope.launchWhenStarted {
      viewModel.setTripDeliver(
           verificationCode,
           remark
      )
  }
  </code></pre>
  <p>Method to collect flow <code>collectTripReattempt()</code></p>
  <pre><code>private suspend fun collectTripReattempt() {
          viewModel.tripReattempt.collect {
              when (it) {
                  is Resource.Initialize -&gt; {
  
              }
              is Resource.Loading -&gt; {
                  Log.i(&quot;???&quot;,&quot;collectTripReattempt loading&quot;)
                  handleSaveEarly()
              }
              is Resource.Success -&gt; {
                  val error = it.data?.error
                  if (error == null) {
                      Tools.showToast(requireContext(), &quot;Success Reattempt&quot;)
                      Log.i(&quot;???&quot;,&quot;collectTripReattempt Success&quot;)
               
                  } else {
                      Tools.showToast(requireContext(), &quot;$error&quot;)
                  }
                  handleSaveEnding()
              }
              is Resource.Error -&gt; {
                  handleSaveEnding()
              }
          }
      }
  }
  </code></pre>
  <p>Below codes are from <code>ViewModel</code>.</p>
  <pre><code>private val _tripDeliver =
      MutableStateFlow&lt;Resource&lt;TripDeliverResponse&gt;&gt;(Resource.Initialize())
  val tripDeliver: StateFlow&lt;Resource&lt;TripDeliverResponse&gt;&gt; = _tripDeliver
  </code></pre>
  <p>This method to call repository.</p>
  <pre><code>suspend fun setTripDeliver(
      verificationCode: String?,
      remark: String?
  ) {
      _tripDeliver.value = Resource.Loading()
      try {
          val result = withContext(ioDispatcher) {
              val tripDeliverParameter = DeliverParameter(
                  verificationCode,
                  remark
              )
              val response = appRepository.setTripDeliver(tripDeliverParameter)
              Resource.getResponse { response }
          }
          _tripDeliver.value = result
      } catch (e: Exception) {
          when (e) {
              is IOException -&gt; _tripDeliver.value =
                  Resource.Error(messageInt = R.string.no_internet_connection)
              else -&gt; _tripDeliver.value =
                  Resource.Error(&quot;Trip Deliver Error: &quot; + e.message)
          }
      }
  }
  </code></pre>
  <p><strong>Logcat</strong></p>
  <pre><code>2021-07-09 19:56:10.946 7446-7446/com.package.app I/???: collectTripReattempt loading
  2021-07-09 19:56:11.172 7446-7446/com.package.app I/???: collectTripReattempt Success
  2021-07-09 19:56:17.703 7446-7446/com.package.app I/???: collectTripReattempt Success
  </code></pre>
  <p>As you can see, the last <code>Success</code> is called again AFTER I pressed back button (<code>popbackstack</code>)</p>
  <p><strong>Question</strong></p>
  <p>How to make it trigger once only? Is it the way I implement it is wrong? Thank you in advance.</p>