Clear focus whenever the root navigation changes (#1411)

This commit is contained in:
David Perez 2024-05-30 16:23:36 -05:00 committed by Álison Fernandes
parent 9b30d50eac
commit 5329629e22

View file

@ -1,10 +1,12 @@
package com.x8bit.bitwarden.ui.platform.feature.rootnav package com.x8bit.bitwarden.ui.platform.feature.rootnav
import android.app.Activity
import androidx.compose.animation.AnimatedContentTransitionScope import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavBackStackEntry import androidx.navigation.NavBackStackEntry
@ -117,6 +119,10 @@ fun RootNavScreen(
} }
previousStateReference.set(state) previousStateReference.set(state)
// In some scenarios on an emulator the Activity can leak when recreated
// if we don't first clear focus anytime we change the root destination.
(LocalContext.current as? Activity)?.currentFocus?.clearFocus()
// When state changes, navigate to different root navigation state // When state changes, navigate to different root navigation state
val rootNavOptions = navOptions { val rootNavOptions = navOptions {
// When changing root navigation state, pop everything else off the back stack: // When changing root navigation state, pop everything else off the back stack: