locked
How to override the device back button?

    Question

  • I am building a Windows Phone 8.1 game and need to cancel the device back button and override it with a custom function.   The app is an HTML5 app and I need to do this with JavaScript.  Does anyone knows how to do this? Or a code sample that I can use for guidance?

    Any help is greatly appreciated.

    Sunday, November 09, 2014 8:51 PM

Answers

  • Nikita,

    Thanks for your help.  I end up implementing my code like this, hope it helps to others.

    HTML
    <div data-win-control="WinJS.Application." data-win-options="{onbackclick : handler}"></div>

    ADDING EVENT
    WinJS.Application.addEventListener("backclick", handler);

    HANDLING EVENT
    function handler(eventInfo){
     //Your code

     // Add return to cancel back button otherwise dont to exit the app
         return true;
    };

    • Marked as answer by KarimFS Thursday, November 13, 2014 5:51 AM
    Thursday, November 13, 2014 5:51 AM
  • HI, KarimFS.

    You can use HardwareButtons class. Only subscribe to  BackPressed event. More details you can find there.

    Regards, Nikita.

    Sunday, November 09, 2014 9:35 PM

All replies

  • HI, KarimFS.

    You can use HardwareButtons class. Only subscribe to  BackPressed event. More details you can find there.

    Regards, Nikita.

    Sunday, November 09, 2014 9:35 PM
  • Nikita,

    Many thanks for your reply, this was very helpful.  I am able to subscribe and execute a function but still the back button event happens and in my case the app is terminated.  How can I stop the back button event?

    Regards,

    Karim

    Sunday, November 09, 2014 10:27 PM
  • You can handle this event.

    e.Handle = true;

    Regards, Nikita.

    Monday, November 10, 2014 5:57 PM
  • Nikita,

    Thanks for your help.  I end up implementing my code like this, hope it helps to others.

    HTML
    <div data-win-control="WinJS.Application." data-win-options="{onbackclick : handler}"></div>

    ADDING EVENT
    WinJS.Application.addEventListener("backclick", handler);

    HANDLING EVENT
    function handler(eventInfo){
     //Your code

     // Add return to cancel back button otherwise dont to exit the app
         return true;
    };

    • Marked as answer by KarimFS Thursday, November 13, 2014 5:51 AM
    Thursday, November 13, 2014 5:51 AM