5
votes

Ionic version: 3.x

Current behavior: After registering the hardware back then if ads are displayed, pressing the hardware back exits the app and does not bind to the normal behavior

Expected behavior: Hardware back should execute the registered functions

Steps to reproduce:

  1. Create a new app with any template add.
  2. Add the admob-free plugin
  3. Register hardwarback to log something(Not to exit).
  4. Display ads.
  5. Press hardware back

Related code:

In the constructor of app.component.ts register the back handler

    platform.registerBackButtonAction(() => {
    console.log("Back Pressed");
  });

Display banner ads or interstitials then without pressing anything in the app press the back button. The app will exit. We are not exiting the app in our handler.

APP REPO HERE.

A related issue can be seen here.

Ionic Info:

cli packages: (C:\Users\prantikv\AppData\Roaming\npm\node_modules)

    @ionic/cli-utils  : 1.12.0
    ionic (Ionic CLI) : 3.12.0

global packages:

    cordova (Cordova CLI) : 7.0.1

local packages:

    @ionic/app-scripts : 3.0.0
    Cordova Platforms  : android 6.0.0
    Ionic Framework    : ionic-angular 3.7.1

System:

    Android SDK Tools : 25.2.5
    Node              : v6.11.3
    npm               : 5.4.2
    OS                : Windows 10

Misc:

    backend : pro
1
the solution provided in the reference didnot solved your issue? - The Blue Shirt Developer
Not for me. Also, that is something that requires editing of the cordova files. Which is not a permanent solution on the developer side. - krv
its not editing in cordova files , its making custom plugin for your own application , and its safe to use, i have modified some plugin to fit my need. - The Blue Shirt Developer
The solution provided is for another admob plugin. I have referenced it because there is the same issue with the plugin currently being used by me, which is the admob-free plugin. I tried the solution and it did not work for me, maybe because the solution is tailored to that plugin and not to the one I am using. Can suggest any changes. I am ok with changes to the plugin files if need be - krv

1 Answers

1
votes

Add your addeventlistener to the document, and capture the event in the capture phase. It will be useful to stopPropagation when the event is fired, to prevent other event listeners from firing.

document.addEventListener("backbutton", onBackKeyDown, true);