使用ionic 4,尝试在使用硬件后退按钮按下事件关闭应用程序之前向用户发出退出警报消息 [英] Using ionic 4,Trying to give an exit alert message to the user before the app gets closed using hardware back button press event

查看:154
本文介绍了使用ionic 4,尝试在使用硬件后退按钮按下事件关闭应用程序之前向用户发出退出警报消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最初的阶段.我正在尝试提供退出应用-是/否? ‘当用户从登录页面或主页(登录后)按下硬件后退按钮时发出警报.我面临的问题是,当我按下后退"按钮时,不仅在登录页面或主页上,退出警告消息都会显示在每个页面上.此外,无论我是否在警报框中按否"选项,都会自动向后导航. ] 抱歉如果做错了什么,这是我的第一篇文章. 我在下面附加的代码-

On my very initial stage.I am trying to give an ‘Exit the App - Yes/No? ‘ alert when the user presses the hardware back button either from the Login Page or from the home page (after login). The problem I am facing is that the Exit alert message appears on every page when I press the back button and not simply on the Login or home page. Moreover automatically navigating backwards regardless if I press ‘No’ option in the alert box. ] Apologies if have done something wrong I this my first post here. The code I using attaching below-

import { Component, OnInit } from '@angular/core';
import{ Router } from '@angular/router';//
import { AlertController } from '@ionic/angular';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-streams-list',
  templateUrl: './streams-list.page.html',
  styleUrls: ['./streams-list.page.scss'],
})
export class StreamsListPage implements OnInit {

  subscripcion: Subscription;

  constructor(
    public router:Router,
    public alertController: AlertController) {}

  ngOnInit() {

    this.getBackButtonClick();

  }


  getBackButtonClick(){
    this.subscripcion = this.platform.backButton.subscribe(()=>{
        //navigator['app'].exitApp();
        this.ClosingApp();
    });
 }



  async ClosingApp()
 {
      let alert = await this.alertController.create({
      header: 'Confirm',
      message: 'Message to confirm!!!',
      buttons: [{
        text: "OK",
        handler: () => { this.exit() }
      }, {
        text: "Cancel",
        role: 'cancelar',
      }]
    })
     alert.present();
 }

 exit()
 {
  navigator["app"].exitApp();
 }


}

推荐答案

您可以尝试基于路由的这种方法.在app.component.ts

You can try this approach based on routing. in app.component.ts

constructor(private router: Router){}

在您的getBackButtonClick()

 getBackButtonClick(){
  this.subscripcion = this.platform.backButton.subscribe(()=>{
   if (this.route.url == '/login' || this.route.url == '/home'){ //please change if your path name is different for login and home
     this.ClosingApp();
    }
 });
}

您还需要取消订阅

  ngOnDestroy() {
   this.platform.backButton.unsubscribe();
}

这篇关于使用ionic 4,尝试在使用硬件后退按钮按下事件关闭应用程序之前向用户发出退出警报消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆