From 095ce01fdd0c5da43ceb6d053487064b0588d3bc Mon Sep 17 00:00:00 2001 From: Maxr1998 Date: Thu, 24 Aug 2023 23:02:06 +0200 Subject: [PATCH] Fix app returning to WebViewFragment when activity restarts `repeatOnLifecycle(Lifecycle.State.STARTED)` caused the flow collection to be restarted after the activity was stopped and then started again (for example, when opening another activity on top and then going back). However, this triggered `handleServerState` with the current state again, which unintentionally replaced the current fragment (for example the player or settings) with the WebViewFragment. The solution is to permanently collect the latest server state, but delay its application to the lifecycle start of the activity. --- app/src/main/java/org/jellyfin/mobile/MainActivity.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/jellyfin/mobile/MainActivity.kt b/app/src/main/java/org/jellyfin/mobile/MainActivity.kt index 512f090a88..e70f4355aa 100644 --- a/app/src/main/java/org/jellyfin/mobile/MainActivity.kt +++ b/app/src/main/java/org/jellyfin/mobile/MainActivity.kt @@ -12,9 +12,9 @@ import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen -import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.repeatOnLifecycle +import androidx.lifecycle.withStarted +import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch import org.jellyfin.mobile.events.ActivityEventHandler import org.jellyfin.mobile.player.cast.Chromecast @@ -88,8 +88,8 @@ class MainActivity : AppCompatActivity() { // Load UI lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.STARTED) { - mainViewModel.serverState.collect { state -> + mainViewModel.serverState.collectLatest { state -> + lifecycle.withStarted { handleServerState(state) } }