Skip to content

Android HardwareBackButton closes app  #20713

@Gemeapp

Description

@Gemeapp

There has been a lot of bug reports about the HardwareBackButton, and once there was even a consolition issue: #16611 but it is locked so only collaborators can discuss it.

I have for some time been struggling to get it to work in my app as required by the designer.

So I created a new app using latest version of ionic (and cordova)

Building and deploying the starter app with tabs on Android, and the HardwareBackButton had no functionality, and it did not matter if I changed

IonicModule.forRoot({hardwareBackButton : true})  // or false

But the interesting thing is when I use the recommended listeners for the HardwareBackbutton like this

this.platform.backButton.subscribeWithPriority(0, () => {
  .. do the handling
})

some strange things started happening.
1: If starting the app and using HardwareBackButton as first thing, the app closes and the event is not triggered
2: If navigating between tabs and then using the HardwareBackButton the event is triggered

and interesting is that adding

IonicModule.forRoot({hardwareBackButton : false})

did not change the behaviour from point 1, but now suddenly it is possible to navigate back to previous tab.

I assume that with {hardwareBackButton : false} the HardwareBackButton is handled by the browser and thus navigating back between the tabs.

After testing more and trying to verify my observations I found that:

Directly After install of the app - HardwareBackButton does nothing
Second time the app is started - HardwareBackButton closes the app without firing any events

Ionic version:

[x] 5.x

Current behavior:

See above

Expected behavior:

Hard to tell if it is a bug, but it is inconsistent - basically it is just important, that the app is not closed without giving the app a possibility to save/ask etc the user before the app is closed

Steps to reproduce:

Related code:

I created an application with this command

ionic start HBB tabs
ionic cordova run android

Noticed no functionality of HardwareBackButton - and later I found that the second time the app is started the HardwareBackButton closes the app if used as first user action

then i added

    console.log("setupBackButton()");
    this.platform.backButton.subscribeWithPriority(0, () => {
      console.log("Hardware Back pressed - subscribed to changes", this.router.url);
    });

in app.component.ts

and finally I changed

IonicModule.forRoot({hardwareBackButton : false})

in app.module.ts

Other information:

Ionic info:

Ionic:

   Ionic CLI                     : 6.1.0 (C:\Users\thorvald.AALBORGDATA\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework               : @ionic/angular 5.0.4
   @angular-devkit/build-angular : 0.803.25
   @angular-devkit/schematics    : 8.3.25
   @angular/cli                  : 8.3.25
   @ionic/angular-toolkit        : 2.2.0

Utility:

   cordova-res : not installed
   native-run  : 0.2.5

System:

   NodeJS : v12.16.1 (C:\Program Files\nodejs\node.exe)
   npm    : 6.13.4
   OS     : Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions