diff --git a/src/libs/Navigation/setupCustomAndroidBackHandler/index.android.ts b/src/libs/Navigation/setupCustomAndroidBackHandler/index.android.ts index 10aa8b99a48..107dae2bb74 100644 --- a/src/libs/Navigation/setupCustomAndroidBackHandler/index.android.ts +++ b/src/libs/Navigation/setupCustomAndroidBackHandler/index.android.ts @@ -13,8 +13,7 @@ type SearchPageProps = StackScreenProps { const rootState = navigationRef.getRootState(); - - const bottomTabRoute = rootState.routes.find((route) => route.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR); + const bottomTabRoute = rootState?.routes?.find((route) => route.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR); const bottomTabRoutes = bottomTabRoute?.state?.routes; const focusedRoute = findFocusedRoute(rootState); @@ -23,7 +22,7 @@ function setupCustomAndroidBackHandler() { return false; } - const isLastScreenOnStack = bottomTabRoutes.length === 1 && rootState.routes.length === 1; + const isLastScreenOnStack = bottomTabRoutes.length === 1 && rootState?.routes?.length === 1; if (NativeModules.HybridAppModule && isLastScreenOnStack) { NativeModules.HybridAppModule.exitApp(); @@ -35,7 +34,7 @@ function setupCustomAndroidBackHandler() { navigationRef.dispatch({...StackActions.pop(), target: bottomTabRoute?.state?.key}); navigationRef.dispatch({...StackActions.pop()}); - const centralPaneRouteAfterPop = getTopmostCentralPaneRoute({routes: [rootState.routes.at(-2)]} as State); + const centralPaneRouteAfterPop = getTopmostCentralPaneRoute({routes: [rootState?.routes?.at(-2)]} as State); const bottomTabRouteAfterPop = bottomTabRoutes.at(-2); // It's possible that central pane search is desynchronized with the bottom tab search. @@ -57,7 +56,7 @@ function setupCustomAndroidBackHandler() { // It's possible that central pane search is desynchronized with the bottom tab search. // e.g. opening a tab different than search will wipe out central pane screens. // In that case we have to push the proper one. - if (bottomTabRoutes && bottomTabRoutes?.length >= 2 && bottomTabRoutes[bottomTabRoutes.length - 2].name === SCREENS.SEARCH.BOTTOM_TAB && rootState.routes.length === 1) { + if (bottomTabRoutes && bottomTabRoutes?.length >= 2 && bottomTabRoutes[bottomTabRoutes.length - 2].name === SCREENS.SEARCH.BOTTOM_TAB && rootState?.routes?.length === 1) { const {policyID, ...restParams} = bottomTabRoutes[bottomTabRoutes.length - 2].params as SearchPageProps['route']['params']; navigationRef.dispatch({...StackActions.push(SCREENS.SEARCH.CENTRAL_PANE, {...restParams, policyIDs: policyID})}); navigationRef.dispatch({...StackActions.pop(), target: bottomTabRoute?.state?.key});