Control from Android Back Keys

Asked 2 years ago, Updated 2 years ago, 89 views

When the Android hardware key, the back key, is pressed, I want to display a dialog and close the application, but it doesn't work properly even if I use the code below.

 document.addEventListener("backbutton", onBackPressed, false);

  function onBackPressed()
  {
    confirmExit();
  }

  function confirmCallback(id)
  {
    if(1==id)
    {
      navigator.app.exitApp();
    }
  }
  function confirmExit()
  {
    navigator.notification.confirm("Do you want to exit the application?", confirmCallback, "End Menu", "End, Cancel")
  }

Shut down the application?The dialog is true, but the app ends before you choose to exit.
When I started the application again, the necessary js files were not loaded.
An It works with AngularJS, but the variables do not change, so I am determined to do so

 document.addEventListener(
    "device ready",
    function()
    {
      document.addEventListener("backbutton", onBackPressed, false);
    }
    , false
  );

In the first place, no dialogs are displayed for sources such as those listed above.

Plug-ins such as notification and exitapp are installed

Please let me know if there is any solution.

android monaca

2022-09-30 20:17

1 Answers

This is how you can disable the backKey operation.

@Override
  public boolean dispatchKeyEvent(KeyEvent){
    // What to do when the back button is pressed?
    if(e.getKeyCode()==KeyEvent.KEYCODE_BACK&e.getAction()==KeyEvent.ACTION_DOWN){
      Log.d("dispatchKeyEvent", "backbutton pressed");

      // Disable backKey operation by returning true
      return true;
    }

    // If it's not a back button, it works normally.
    return super.dispatchKeyEvent(e);
  }


2022-09-30 20:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.