Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic Capacitor hardware back button is automatically closing the app

My current setup is:

@capacitor/core: 3.0.0, @ionic-native/core: 5.0.7

I'm trying to change the behavior of my app to not close the app, but go back in the navigation stack. To my knowledge, the hardware back button on Android devices did not automatically close the app until I upgraded Capacitor to 3.0.0

What is confusing me though, is how I have absolutely 0 code for handling the back button functionality, and from everything I'm searching online shows the back button doing nothing by default, not automatically closing the app as the default (as mine seems to be doing). I've searched all the project files for anything to do with "platform", "backButton", and "App.Exit" and was unable to find any code that may be causing the app to close.

I've tried subscribing to the back button press event using the below code and it is never ran. The app closes instead of showing the alert dialog. I've changed the priority from 0, 10, and 99 (all priorities listed in the Ionic documentation)

this.platform.backButton.subscribeWithPriority(10, () => {
  alert('Back button pressed!');
});
like image 416
kasprdev Avatar asked May 30 '21 23:05

kasprdev


People also ask

How do you handle hardware back button in Ionic 4?

The hardware back button is found on most Android devices. In native applications it can be used to close modals, navigate to the previous view, exit an app, and more. By default in Ionic, when the back button is pressed, the current view will be popped off the navigation stack, and the previous view will be displayed.

How do you close an app on back pressed in Ionic?

backButton. subscribeWithPriority(99999, () => { navigator['app']. exitApp(); });

Can I use Capacitor without Ionic?

Do I need to use Ionic Framework with Capacitor? No, you do not need to use Ionic Framework with Capacitor. Without the Ionic Framework, you may need to implement Native UI yourself. Without the Ionic CLI, you may need to configure tooling yourself to enable features such as livereload.

How does Cordova handle back button?

Handling Back Button To be able to implement your own functionality, you first need to disable exiting the app when the back button is pressed. Now when we press the native Android back button, the alert will appear on the screen instead of exiting the app. This is done by using e. preventDefault().


2 Answers

  1. Install capacitor app.

    npm install @capacitor/app

  2. Import it.

    import { App as CapacitorApp } from '@capacitor/app';

  3. Add back listener if can go back then we can push to back or exit the app.

    CapacitorApp.addListener('backButton', ({canGoBack}) => {
      if(!canGoBack){
        CapacitorApp.exitApp();
      } else {
        window.history.back();
      }
    });
    
like image 66
Siyahul Haq Avatar answered Sep 20 '22 19:09

Siyahul Haq


So, I feel a bit dumb after realizing this, but it is because I had to run the below commands, because I apparently didn't update them when upgrading Capacitor a while back. Make sure all of your plugins are fully updated, yours may be different than mine.

npm install @capacitor/app
npx cap sync
like image 23
kasprdev Avatar answered Sep 19 '22 19:09

kasprdev