1
votes

I am using Ionic version 5.4.16 and i am trying to close the app by hardware back button with a alert message for confirm to exit. I have checked a lot of tutorials for the same and everywhere i saw 2 ways to achieve that --

this.platform.exitApp();

and

navigator.app.exitapp()

and

navigator.["app"].exitapp();

But none of these option are working in Ionic 5, its not recognizing the function exitApp(). So how to achieve the same if anyone has faced the same issue do suggest, thank you.

2
navigator["app"].exitApp(); - Mostafa Harb
Hi @MostafaHarb, from yesterday itself i was trying to communicate with u with my new question, but i didn't found any options to contact stackoverflow members. Anyway, thanks for replying, but using ur suggestion the error is still occuring, if i am keeping app in double quote then it says to keep in single quote, if i keep it in single quote then it says object shouldn't be in single quote, if i remove single quote, then it says app value not found. - Ritesh gupta
npmjs.com/package/cordova-plugin-exit , use this plugin , for me i didn't test it yet but found from some days , i read it have 2 issues although there where 400 installers per week so i think the 2 rhat posted it maybe have some peoblems since others didn't stop asking on it. Any way test it you will lose nothing .and to reach me, if you want my email tell me and if you need whatsapp also tell me.. - Mostafa Harb
ok bro, let me try, and your emailid will be fine, i will post the questions here and will share u the link in email. :) - Ritesh gupta

2 Answers

0
votes

To close the app, you must configure the following:

import { Plugins } from '@capacitor/core';

const { App } = Plugins;

App.exitApp();
0
votes

Try to paste this code.

 public unsubscribeBackEvent: any;
    
      ionViewDidEnter() {
    
        this.initializeBackButtonCustomHandler();
    
      }
    
    initializeBackButtonCustomHandler(): void {
    
        this.unsubscribeBackEvent = this.platform.backButton.subscribeWithPriority(999999,  () => {
    
     
    
            if(window.confirm('Do you want to exit the app?'))
    
            {
    
              navigator['app'].exitApp();
    
            }
    
        });
    
    
      }
    
    
        ionViewWillLeave() {
    
          this.unsubscribeBackEvent.unsubscribe();
    
        }
    

I hope it's done.