How do I listen for for a Android back button with Capacitor Browser Plugin

Viewed 26

I have an ionic App that has an external webpage it links to. I can listen for browser closing, but I also wish to listen for the user using the Android back button to return return to the App. here is the code I use.

import { Browser } from '@capacitor/browser';

viewWebPage = async ( url: string ) => {
  url = this.LINK
  await Browser.open( { url: url } );
  Browser.addListener('browserFinished', () => {
    console.log("browser finished");
  });
  // add listener for back button
};
1 Answers

Turns out if you use the Android back button to completely exit the open browser window the Capacitor Browser plugin will capture the event with

import { Browser } from '@capacitor/browser';

viewWebPage = async ( url: string ) => {
  url = this.LINK
  await Browser.open( { url: url } );
  Browser.addListener('browserFinished', () => {
    console.log("browser exited");
  });
};
Related