Ionic 2: How to handle the hardware back button which checks confirmation of Exit in app

前端 未结 7 651
你的背包
你的背包 2020-12-05 06:12

I am facing an issue how to handle the default mobile\'s back button which checks the confirmation while exiting from the application, if I pressed the back button there sho

7条回答
  •  忘掉有多难
    2020-12-05 06:31

    Kinda late to the party... But there is more to back button other than closing the pushed pages, specially for project with several tab pages.

    Sometimes a page is pushed NOT into the root page, but in one of the tab page's navCtrl. So we have to check for all of them.

    Also, if no page or menu is open, we should circle around the most recently used tabs (similar to Instagram app) and go back to the previous tab. Moreover, we should not go back to each tab more than once (similar to Instagram)

    I got inspired by the answers here and created a comprehensive method that handles all necessary features:

    • Side menu: Closes the side menu if it was open
    • Pushed pages: Closes any pages that might have been pushed on any of the tab pages’ nav controller
    • Switch between tabs: If the menu was closed, and there was no pushed page, it should take the user back to the previous recently used tab using a stack.
    • Show alert: If there was no previous recently used tab, an alert box should ask the user if they want to exit.

    The details are in this blog post

    A demo code can be downloaded from my github.

提交回复
热议问题