Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 48980

Android hardware back button not working in ionic v5

$
0
0

@ViewChildren(IonRouterOutlet) routerOutlets: QueryList;

lastTimeBackPress = 0;
timePeriodToExit = 2000;

constructor(
private router: Router,
private alertController: AlertController,
private location: Location,
private platform: Platform,
) {
this.backButtonEvent();
}

backButtonEvent() {

this.platform.backButton.subscribeWithPriority(10, () => {

  alert('back button pressed');

  this.routerOutlets.forEach(async (outlet: IonRouterOutlet) => {

    if (this.router.url != '/home') {

      // await this.router.navigate(['/']);

      await this.location.back();

    } else if (this.router.url === '/home') {

      if (new Date().getTime() - this.lastTimeBackPress >= this.timePeriodToExit) {

        this.lastTimeBackPress = new Date().getTime();

        this.presentAlertConfirm();

      } else {

        navigator['app'].exitApp();

      }

    }

  });

});

}

async presentAlertConfirm() {

const alert = await this.alertController.create({

  // header: 'Confirm!',

  message: 'Are you sure you want to exit the app?',

  buttons: [{

    text: 'Cancel',

    role: 'cancel',

    cssClass: 'secondary',

    handler: (blah) => { }

  }, {

    text: 'Close App',

    handler: () => {

      navigator['app'].exitApp();

    }

  }]

});

await alert.present();

}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 48980

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>