Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

back button in cordova/phongap meteor build for android won't close application consistently

I've built and app for android with meteor and phonegap/cordova: https://play.google.com/store/apps/details?id=com.idqkd3gsl4qt51152xgy

It works decently OK (especially given that I'm not really a programmer), but one UX issue I've been struggling with is that the app will not consistently close when pressing the back button on my phone. Every once in a while it works on the first press, but most of the time I have to jam it 5-6 times in a row to get the app to close.

I'm using the latest iron-router. The rest of the packages I'm using don't seem particularly relevant to this issue but they are as follows just in case:

standard-app-packages coffeescript natestrauser:[email protected] accounts-password aldeed:autoform aldeed:collection2 nemo64:bootstrap less alanning:roles joshowens:accounts-entry mrt:accounts-admin-ui-bootstrap-3 mizzao:jquery-ui iron:router sacha:spin raix:push mizzao:bootboxjs meteorhacks:kadira bootstrap3-media-query

the repo can be seen here: https://github.com/The3vilMonkey/BrewsOnTap

like image 243
funkyeah Avatar asked Jan 20 '15 21:01

funkyeah


1 Answers

I can't specify the exact reason for this issue other than it seems like at startup there is a sort of redirect happening with cordova apps. Best solution I've found is to catch the popstate event and when you get back to the beginning exit or suspend the application.. I prefer suspend that way when the user comes back to the app it doesn't start it's lifecycle from the beginning.

if(Meteor.isCordova){
    Meteor.startup(function(){
        window.onpopstate = function () {
            if (history.state && history.state.initial === true){
                navigator.app.exitApp();

                //or to suspend meteor add cordova:[email protected]
                //window.plugins.Suspend.suspendApp();
            }
        };
    });
}

One caveat that got me when I first used this was redirecting to a login page if the user wasn't logged in.. If you're using this method in an app that does this, you'll want to switch to rendering the login page in place rather than redirecting otherwise your app will exit/suspend immediately.

like image 166
Kelly Copley Avatar answered Oct 26 '22 19:10

Kelly Copley