Change function of Handy back button in the App

  • Ralf
    Participant
      5 years, 5 months ago #18884

      I want to use the “back button” on my handy to go back one site or one screen , but if i use it , the app wants to end the app.

      can anybody help?

      jonas.rausch
      Participant
        Has successfully completed the online course Introduction
        Has successfully completed the online course Intermediate (200)
        Has successfully completed the online course Advanced (300)
      5 years, 5 months ago #18890
      Up
      2
      Down
      ::

      Hi Ralf,

       

      The Device Button events like “back” and “home” can be detected and overwritten via JQuery/JS:

       

      You can add this line of code to the onInit event pf your first screen:

      document.addEventListener(‘backbutton’, function(){
      //do stuff here
      }

      Be Aware that overwriting the event will supress the native functions of the button, if you want to exit the app, you will need to code that yourself.

       

      Dinkel
      Moderator
        Has successfully completed the online course Introduction
        Has successfully completed the online course Intermediate (200)
        Has successfully completed the online course Advanced (300)
        Has successfully completed the online course Basics (100)
      5 years, 4 months ago #19490
      Up
      2
      Down
      ::

      You have to bind this.

      document.addEventListener("backbutton", onBackKeyDown.bind(this), false);
      
      function onBackKeyDown() {
          var curr_page = sap.ui.getCore().byId("Application").getCurrentPage();
          console.info(curr_page.getId());
          if (curr_page.getId() === "PAGE1") {
              console.info('close pressed');
              window.navigator.app.exitApp(); // OK
          } else {
              console.info('back pressed');
              // window.navigator.app.backHistory(); // <no action
              this.navBack(); // Uncaught TypeError this.navBack is not a function
              // window.navigator.app.navBack();  // Uncaught TypeError window.navigator.app.navBack is not a function
          }
      
      }
      Dinkel
      Moderator
        Has successfully completed the online course Introduction
        Has successfully completed the online course Intermediate (200)
        Has successfully completed the online course Advanced (300)
        Has successfully completed the online course Basics (100)
      5 years, 4 months ago #19469
      Up
      1
      Down
      ::

      To navigate back you can use
      this.navBack();

      and to close the application you can use
      window.navigator.app.exitApp(success,error)

      jonas.rausch
      Participant
        Has successfully completed the online course Introduction
        Has successfully completed the online course Intermediate (200)
        Has successfully completed the online course Advanced (300)
      5 years, 4 months ago #19473
      Up
      1
      Down
      ::

      Hi Ralf,

      You can get the name of the current screen with this command:

      sap.ui.getCore().byId(“Application”).getCurrentPage()

      It will yield an Object containing various metadata of the Current Page, including the name.

       

      To navigate, you can use dinkel´s snipptets.

      Ralf
      Participant
        5 years, 4 months ago #19489
        Up
        1
        Down
        ::

        Ok, this Script works,

        document.addEventListener("backbutton", onBackKeyDown, false);
        
        function onBackKeyDown() {
            var curr_page = sap.ui.getCore().byId("Application").getCurrentPage();
            console.info(curr_page.getId());
            if (curr_page.getId() === "PAGE1") {
                console.info('close pressed');
                window.navigator.app.exitApp(); // OK
            } else {
                console.info('back pressed');
                // window.navigator.app.backHistory(); // <no action
                this.navBack(); // Uncaught TypeError this.navBack is not a function
                // window.navigator.app.navBack();  // Uncaught TypeError window.navigator.app.navBack is not a function
            }
        
        }
        

        but this.navBack(); and the other variations doesn´t.
        Any Idea why?

        Ralf
        Participant
          5 years, 4 months ago #19443
          Up
          0
          Down
          ::

          That´s it,

          i change it oninit Screen2 to back function…. and oninit Screen1 back to normal

           

          Ist das Script getestet? Irgendwie läuft das bei mir nicht wie gewünscht

          jonas.rausch
          Participant
            Has successfully completed the online course Introduction
            Has successfully completed the online course Intermediate (200)
            Has successfully completed the online course Advanced (300)
          5 years, 4 months ago #19457
          Up
          0
          Down
          ::

          Hi,

           

          I wrote that snippet from memory, but it should work.

          Can you describe what´s happening?

          And what Os does your device run?

           

           

          Ralf
          Participant
            5 years, 4 months ago #19467
            Up
            0
            Down
            ::
            document.addEventListener("backbutton", onBackKeyDown, false);
            function onBackKeyDown() {
            
            // Handle the back button
            //alert('Back Button is Pressed!');
            
            if ("{actual Screen}"=== "Main")
            {
            console.info('close pressed');
            // Backbutton -> close App
            }
            else
            {
            console.info('back pressed');
            // NavTo history.back or backtoMain
            
            }
            
            }

            This works, but i don´t know how to handle the if-command and Backbutton in SAPUI…

            Need help how i ask for actual Screen and the commands how i go history (window) back and close app

             

            Ralf
            Participant
              5 years, 4 months ago #19520
              Up
              0
              Down
              ::

              ^^That´s it…. Thx a lot

              greets

            Viewing 10 posts - 1 through 10 (of 10 total)

            You must be logged in to reply to this topic.