Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap - navigator.app.backHistory() not working on HTML back button

In my app i am using phonegap 2.6.For back button, I am using the following function

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

function onBackKeyDown() {
    alert("hello");
    navigator.app.backHistory();
}

document.addEventListener('deviceready', onDeviceReady, true);

The above function works fine when I click on the device's hardware back button. But when I click on the back button it is not working.

I have designed my back button as below:

<a class="ui-link" href="#" rel="external" onclick="onBackKeyDown()">
        <img src="images/icon-back.png" alt="Phone" border="0">
</a>

But this button is working fine for this navigator.app.exitApp(); (application exit).

//Working Fine
function onBackKeyDown() {
    navigator.app.exitApp();
}

//Not Working
function onBackKeyDown() {
    navigator.app.backHistory();
}

but not working for navigator.app.backHistory();.

like image 532
Iam4fun Avatar asked May 14 '13 11:05

Iam4fun


2 Answers

I have tried 3 separate things when I faced the same situation:

  • window.history.back()

  • navigator.app.backHistory();

  • History.go(-1);

Individually, none of these solve the problem. I put all 3 things together and much to my surprise it worked. I really don't know what is behind it.

Then I decreased to two functions and removed:

  • window.history.back()

Now I am using this function and it is working fine.

//Works Fine
function onBackKeyDown() {
    history.go(-1);
    navigator.app.backHistory();
}
like image 155
sdg Avatar answered Oct 26 '22 04:10

sdg


If you use the attribute data-rel="back" on an anchor, any clicks on that anchor will mimic the back button, going back one history entry and ignoring the anchor's default href.

like image 36
Mehmet Avatar answered Oct 26 '22 04:10

Mehmet