Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova/Phonegap - Does beforeunload work or any alternatives?

So I have a client object I want to write to a file before the app is killed. Does anyone know if the browser beforeunload event works for that purpose? Or does Cordova have a native way to handle that condition?

I know there are the "pause" and "resume" events, but I'm not sure if those handle the "kill" event as well. I can't seem to find any documentation on this anywhere.

If anyone can point to the right direction of have a recommendation it would be appreciated.

like image 665
gabaum10 Avatar asked Oct 19 '22 15:10

gabaum10


1 Answers

After a little bit more digging and some trial and error, I guess the proper way to handle this is to listen to the "pause" event I mentioned in my question. It seems to fire every time the user even pulls up the menu to kill the app. It should work then for my purposes.

document.addEventListener("pause", function () {
    app.inBackground = true;
    app.saveData();
}, false);

I'm not sure if it follows any standard or there is a better way to do it, so if anyone else has more info, feel free to leave it here.

like image 194
gabaum10 Avatar answered Oct 21 '22 04:10

gabaum10