ionic-framework: v4: Android hardware back button doesn't work

Bug Report

Ionic Info Run ionic info from a terminal/cmd prompt and paste the output below.

Ionic:

   ionic (Ionic CLI)          : 4.0.0-rc.9 (C:\Users\ape\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework            : @ionic/angular 4.0.0-alpha.7
   @angular-devkit/core       : 0.6.8
   @angular-devkit/schematics : 0.6.8
   @angular/cli               : 6.0.8
   @ionic/ng-toolkit          : not installed
   @ionic/schematics-angular  : 1.0.0-rc.9

Cordova:

   cordova (Cordova CLI) : 8.0.0
   Cordova Platforms     : android 6.3.0 ios 4.5.4

System:

   Android SDK Tools : 25.2.5
   NodeJS            : v10.5.0 (C:\Program Files\nodejs\node.exe)
   npm               : 6.1.0
   OS                : Windows 10

Describe the Bug Android hardware button doesn’t work as intended. It seems to be because of back button behavior overriding in generated cordova.js:

var backButtonChannel = cordova.addDocumentEventHandler('backbutton');
        backButtonChannel.onHasSubscribersChange = function() {
            // If we just attached the first handler or detached the last handler,
            // let native know we need to override the back button.
            exec(null, null, APP_PLUGIN_NAME, "overrideBackbutton", [this.numHandlers == 1]);
        };

which is caused by:

https://github.com/ionic-team/ionic/blob/181cd559cc68542d48a33e6ff58408e2ffc2077c/angular/src/providers/platform.ts#L42

Steps to Reproduce Steps to reproduce the behavior:

  1. Clone repo https://github.com/ionictheme/ionic4-start-theme
  2. Install deps and run on device
  3. Click on android hardware back button
  4. Nothing happens

Expected Behavior Android hardware back button should return the previous page or exit app.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 10
  • Comments: 19 (2 by maintainers)

Most upvoted comments

this.platform.exitApp(); doesn’t work for ionic v4.1.2

@manucorporat PR https://github.com/ionic-team/ionic/pull/15615 fixes the remaining problem with the handler registered by overlays.tsx.

There are other cases as well, such as Back button for modal does’t work either.

Just keep this in mind that the solution on stackoverflow is a temporary fix. we should wait for permanent fix from ionic team, which I believe should cover all cases and should be available in next updates.