Prevent the acccidental Closing of an App


#1

I am building an app to help boaters track their route and automate the recording of their log books. I have an app that is tracking their location, and I want to prevent accidental closing of the application while tracking is still on. I know this is not good form, but terminating tracking accidentally IS a big issue - this feature I am trying to include is a result of me doing this multiple times while testing.

If I hit the back button from the main menu screen and the app is in tracking mode, I want to pop up a message along the lines of “Do you really want to stop tracking and quit the app?” with a NO/YES option, so if the person does not really want to quit the app they can say NO and the app will then close with the tracking functionality running in the background. YES will stop the tracking and close the app. I purchased an app that helps track your anchoring position that works this way.

Any ideas? I am using Javascript and tried using the NSB.Msgbox function. BTW, MessageBox is a function that calls NSB.MsgBox but allows me to call it in various ways without having a ton of overloaded function names. My code is as follows:

window.onbeforeunload = MessageBox("Are you sure you want to exit app?", "Exit Application?", 4, CheckIfExitResponse);

function CheckIfExitResponse(result){
	if (result == NSB.Yes) {
		return;
	} else {
		return false;
	}
}        

Another puzzling thing; the message pops up when I open the app, NOT when I am pressing the back button to exit.

Thanks in advance :slight_smile:


#2

I used to handle the app exit and looked back through my old code and couldn’t find it… that’s how old the code is.

You can find some fairly recent posts on the topic by searching “phonegap detect app exit” and one such answer is here.

On your home page you really shouldn’t have a back button that allows the user to fully exit the the app. Most people have gotten away from this because it does cause a bad UX.

These days, what you want to do is track the pause/resume events and and the online/offline events. Pause events get triggered when the app goes to the background and resume when it comes back to the foreground. Offline events would be for events like when airplane mode is engaged (which I suspect will cause the same behavior in your app). Online is when service is restored.

  // handle when the device looses it's connection
  document.addEventListener("offline", onOffline, false);
  document.addEventListener("online", onOnline , false);
 
  // handle pause and resume  
  document.addEventListener("pause", onPause, false);
  document.addEventListener("resume", onResume, false);


// this happens when the user switches apps.  Our app has not
// been terminated but is (temporarily) in the background 
function onPause() {
    confirm("Do you really want to stop tracking?");
}

function onResume()
{
  checkConnection();        // make sure we have some sort of wifi or cell access
  gps.checkLocation();      // make sure gps exists and we have permission to use it
}

The other puzzling thing we can’t help you with unless you post some code.


#3

@rossbc, you didn’t mention whether you are running as a PhoneGap app.

If not, there is no way to detect reliably if the user has exited the app. You need to save the data continuously to localStorage or SQLite so it’s there when the app resumes.

If you are in PhoneGap, @PPetree answer is a good place to start.


#4

I am running as a Phonegap app. I have a main menu page for the tasks, each navigational task has its own form with a back button, but the main page does not. I am pressing the back button on my Android toolbar to get out of the app, I think PPeTree 's answer above is sort of what I am looking for. I want the user to be able to back out from an oh#hit press and still reopen the app with the tracking still going.


#5

Thanks! I think this could help - I will give it a try and let you know. Thanks!