From fecd370a25bf29eba53cf0e4b338189e3572cb19 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 c44ef52c..9406b969 100644 --- a/app/src/main/java/org/jellyfin/mobile/MainActivity.kt +++ b/app/src/main/java/org/jellyfin/mobile/MainActivity.kt @@ -15,9 +15,9 @@ import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.fragment.app.Fragment -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 @@ -123,8 +123,8 @@ class MainActivity : AppCompatActivity() { // Load UI lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.STARTED) { - mainViewModel.serverState.collect { state -> + mainViewModel.serverState.collectLatest { state -> + lifecycle.withStarted { handleServerState(state) } }