Skip to content

Commit

Permalink
Fix back button on last route not exiting the app on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding committed Aug 4, 2023
1 parent a786879 commit faf6f7e
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/TabbedRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
IonTabs,
useIonRouter,
} from "@ionic/react";
import { App } from "@capacitor/app";
import {
personCircleOutline,
cog,
Expand All @@ -30,7 +31,7 @@ import SpecialFeedPage from "./pages/shared/SpecialFeedPage";
import styled from "@emotion/styled";
import UserPage from "./pages/profile/UserPage";
import SettingsPage from "./pages/settings/SettingsPage";
import { useContext, useMemo, useRef } from "react";
import { useContext, useEffect, useMemo, useRef } from "react";
import { AppContext } from "./features/auth/AppContext";
import InstallAppPage from "./pages/settings/InstallAppPage";
import SearchPage, { focusSearchBar } from "./pages/search/SearchPage";
Expand All @@ -45,7 +46,7 @@ import RepliesPage from "./pages/inbox/RepliesPage";
import MessagesPage from "./pages/inbox/MessagesPage";
import ConversationPage from "./pages/inbox/ConversationPage";
import InboxPage from "./pages/inbox/InboxPage";
import { IonRouterOutletCustomEvent } from "@ionic/core";
import { BackButtonEventDetail, IonRouterOutletCustomEvent } from "@ionic/core";
import InboxAuthRequired from "./pages/inbox/InboxAuthRequired";
import UpdateAppPage from "./pages/settings/UpdateAppPage";
import useShouldInstall from "./features/pwa/useShouldInstall";
Expand Down Expand Up @@ -98,6 +99,31 @@ export default function TabbedRoutes() {
const actor = location.pathname.split("/")[2];
const iss = useAppSelector(jwtIssSelector);

// Back button handling for Android native app
useEffect(() => {
const backButtonHandler = (ev: CustomEvent<BackButtonEventDetail>) => {
ev.detail.register(-1, () => {
if (!router.canGoBack()) {
App.exitApp();
}
});
};

document.addEventListener(
"ionBackButton",
// eslint-disable-next-line no-undef
backButtonHandler as EventListener
);

return () => {
document.removeEventListener(
"ionBackButton",
// eslint-disable-next-line no-undef
backButtonHandler as EventListener
);
};
}, [router]);

const userHandle = useAppSelector(handleSelector);
const profileLabelType = useAppSelector(
(state) => state.settings.appearance.general.profileLabel
Expand Down

0 comments on commit faf6f7e

Please sign in to comment.