Why the "Back" button does not navigate back to the previous page on my smartphone?

2.5k Views Asked by At

I'm new to Mobile development. I wrote a simple web app for mobile platforms which contains two HTML files: index.html, info.html. In the main page index.html, there is a hyperlink that allows user to navigate to the next HTML page: info.html like this:

<a href="info.html">Next</a>

Both these two HTML files are located in the same directory. I tested it in my desktop browser and it works fine. But problems occurred when I built and loaded the app to my Android smartphone. If I click "Next" link as given above in index.html, it will go the info.html page without any problems. But when I used the "Back" button on my smartphone, it got stuck there and didn't go back to the previous page index.html as expected. So what is the problem here? I'm using MoSync SDK for HTML web app development tool.

2

There are 2 best solutions below

1
On

I don't have an android phone myself. Are you deleting the history by accident, if not try placing your own back button in it might not work but it could help.

Insert this code in the head of your HTML document:

<script>
function goBack()
  {
  window.history.back()
  }
</script>

Then insert this code where you want the button to appear:

<input type="button" value="Back" onclick="goBack()">

To find out more about the javascript back button: http://www.w3schools.com/js/js_window_history.asp

I hope this helps.

0
On

try this, it worked for me

                      ` //back button (on Android).
                      document.addEventListener(
                    "backbutton",
                 function()
                        {
                     window.history.back()
                   },
                   true);`   

AND this one is to exit the app

                    `// Close the application when the back key is pressed.
               document.addEventListener(
                        "backbutton",
                       function()
                     {
                        mosync.app.exit();
                  },
                 false);`

they are both javascripts files so put them in the script tags put this script on the fist page of your android app