ionic-framework: bug: Hardware back button closes app on Android

Bug Report

Ionic version:

[x] 5.x

Current behavior:

When pressing the hardware back button on an Ionic app before doing anything else, it will close the app. This does not happen if you interact with the app in any way first, like touching somewhere on the screen.

Expected behavior:

I would expect the app to not close?

Steps to reproduce:

  1. Create a new cordova project with ionic cli
  2. Run ionic cordova run android
  3. Press hardware back button
  4. App should now close

Related code:

Other information:

I do not know if this is an Ionic issue or if the problem lies somewhere else, but I thought I would give a heads up. Also this doesn’t seem to be listed in the hardware back button consolidated list of issues.

Ionic info:

Ionic:

   Ionic CLI                     : 6.13.1 (C:\Users\<user>\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework               : @ionic/angular 5.6.4
   @angular-devkit/build-angular : 0.1102.8
   @angular-devkit/schematics    : 11.2.8
   @angular/cli                  : 11.1.4
   @ionic/angular-toolkit        : 3.1.1

Cordova:

   Cordova CLI       : 10.0.0
   Cordova Platforms : android 9.0.0
   Cordova Plugins   : not available

Utility:

   cordova-res : not installed
   native-run  : 1.3.0

System:

   Android SDK Tools : 26.1.1 (C:\AndroidSDK)
   NodeJS            : v15.11.0 (C:\Program Files\nodejs\node.exe)
   npm               : 7.6.0
   OS                : Windows 10

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 24 (12 by maintainers)

Most upvoted comments

I can reproduce this behavior, though only on newer versions of Android. In addition, I can reproduce this in a regular Angular app without Ionic Framework, so I need to do some more digging to see what could be causing this issue.

No problem, glad the issue is resolved 😄

Hmmmm okay, capacitor/app actually implement the backbutton event dispatch… I missed this part of the migration documentation all afternoon … Sorry 😦

Did you add the App plugin? https://capacitorjs.com/docs/v3/updating/3-0#migrating-your-app-to-use-the-new-official-plugin-packages

For best performance with Ionic Framework, you should make sure these plugins are installed even if you don’t import them in your app:

npm install @capacitor/app @capacitor/haptics @capacitor/keyboard

I will do that, thank you for the support!

Thanks! I will chat with the Capacitor team and update this thread.

Using this.platform.backButton.subscribe(() => { this.location.back(); }); Or this.platform.backButton.subscribeWithPriority(10000, () => { this.location.back(); });

None actually works on my Android 11 Pixel 2, no matter what i try App will just close.