简体   繁体   中英

Using Ionic (angular).Unable to access Hardware back button of mobile devices which have buttons inside the display/screen

I am facing a problem when I trying to access hardware back button so that I can produce an alert message to the user before the app get closed. Initially I was unable to do , so I raised a question on stack overflow ( link of the question ) and with the help of the answers I almost solved my problem until I observed on some particular devices I unable to access the back button. Noticing those devices which has back button inside the display (I think we can call this type of buttons as soft back button ) in those device I unable to produce alert box,to be more precise unable to access the back button. But If I touch on the screen and than press the button, it is working fine . basically if someone just launch the app and presses the back button the app gets exit.

It is very difficult for me to write and produce my issue properly, therefore I am sharing a link of video please watch and try to understand and ask for any clarification

Link-1, The type of devices on which alert is not working

Link-2 On this type devices working fine

When you are in the app on another screen and press back button that time you go to the back screen. and when your screen is home or login, and that time within two seconds you press twice the time back button app is closed.

public astTimeBackPress = 0;
public timePeriodToExit = 2000;

constructor(
    public toastController: ToastController,
    private platform: Platform,
    private nav: NavController,
    private router: Router,
  ) { }

handleBackButton() {
    this.platform.backButton.subscribe(() => {
      if (this.loaderOff) {
        document.addEventListener(
          'backbutton',
          () => {},
          false);
      } else {
        if (
          this.router.url === '/tabs/home' ||
          this.router.url === '/signin'
        ) {
          if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
            navigator['app'].exitApp();
          } else {
            this.presentToast('Press again to exit');
            this.lastTimeBackPress = new Date().getTime();
          }
        } else {
          this.nav.back();
        }
      }
    });
}

async presentToast(msg, color = 'dark') {
    const toast = await this.toastController.create({
      color,
      message: msg,
      duration: 3000,
      showCloseButton: true,
      closeButtonText: 'Close',
    });
    toast.present();
  }
public void callAlert(){
    AlertDialog.Builder builder1 = new AlertDialog.Builder(appCompatActivity);
    builder1.setMessage("Do you want to close.");
    builder1.setCancelable(true);

    builder1.setPositiveButton(
            "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    finish();
                }
            });

    builder1.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog alert11 = builder1.create();
    alert11.show();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        callAlert();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM