2

I'm aware of the following questions that relate to this, but do not solve the problem I face.

Onsen uI:Controll android backbutton routes

Onsen ui:Disable device backbutton in android didnt work

Here is my code in index.html:

ons.ready(function () {
        ons.disableDeviceBackButtonHandler();
        appnavigator.getDeviceBackButtonHandler().disable();
        
        window.document.addEventListener('backbutton', function (event) {
            if (appnavigator.getPages().length > 1) {
                event.preventDefault();
                appnavigator.popPage();
                console.log('Popped a page and now we have', appnavigator.getPages().length, 'pages');
            } else {
                console.log('No more pages on stack, quitting...');
                navigator.app.exitApp();
            }
        }, false);
    });

I have <ons-navigator id="navigator" var="appnavigator"> in the body.

This pops a page, and then quits the application. Why is that? I'm using Onsen UI v 1.3.14 and Cordova 5.3.3.

Community
  • 1
  • 1
gopalkoduri
  • 442
  • 1
  • 4
  • 15
  • The problem I think is ons handlers are not being disabled at all. I just tried the code with just the statements to disable the handlers, and the app still quits on pressing back button. – gopalkoduri Dec 17 '15 at 14:52
  • 1
    What you are trying is actually the default behavior. `popPage` when there are pages and exit app when the stack is empty. I just tried it in a Cordova app and works well. If you want to disable it, try wrapping `ons.disableDeviceBackButtonHandler()` in a `setTimeout(fn, 0)` and see if it works. If you disable everything it will just exit the app since there is no handler. – Fran Dios Dec 18 '15 at 03:41
  • With none of this code, the app should show default behaviour. But it doesn't though. It quits on pressing back button. – gopalkoduri Dec 19 '15 at 06:50

0 Answers0