10

Hy! I need to catch the exit app event in my phonegap application. Actually I want to trigger a looseLife() function if the player tries to cheat and exit the app with minimize and exit from task manager while he already started a new lvl. If he exit it correctly, by pressing the back button it works fine.

I tried onunload or onbeforeunload but these functions get called only if I close the app normally, without minimizing it and closing it from task manager.

The pause function also doesn't help me because it gets triggered also if the screen is locked or when the user switches to another app and than back again.

How to catch the event when the app is minimized and closed in the task manager?

rashidnk
  • 4,096
  • 2
  • 22
  • 32
Alex Stanese
  • 735
  • 4
  • 16
  • 33
  • http://stackoverflow.com/questions/18931722/exit-button-event-in-phonegap-for-ios – Paolo Bernasconi May 01 '14 at 14:58
  • @PaoloBernasconi I specified in my post that the pause event doesn't help me.. because it gets trigeread also if I lock the screen or I just minimise the app... I need the event when the app is minimised and the user closes it from the task manager! – Alex Stanese May 01 '14 at 16:32
  • Anybody? Please Its very important for me! I can't imagine there's no event or workaround for this issue. – Alex Stanese May 01 '14 at 21:23
  • What's your platform? You didn't explain it in your post. In my previous applications I had override the end event of the android app in native code. – jpgrassi May 02 '14 at 17:20
  • @jpgrassi its on android. And I would like to use online build... so I guess a native code would help me as I upload only the ww folder... – Alex Stanese May 02 '14 at 17:45
  • I really don't see a way of doing it without having to right native code. Don't see a problem having local build in this specific case. – jpgrassi May 02 '14 at 17:50
  • @jpgrassi but if I write natuve code, can it be somehow implemented in te online build? I need the online build because it already have some configurations and for now there are 2 products that differe a little bit from local to online build (because of small changes). And I also would like to compile it for ios online because I dont have a mac currently... – Alex Stanese May 02 '14 at 21:23
  • It will work if you implement it in a plugin I guess. – jpgrassi May 05 '14 at 11:30

4 Answers4

5

I found window.onbeforeunload to work better for cleaning up resources. Possibly off topic but I think window.onbeforeunload wouldn't create any race condition type issues.

Michael Ribbons
  • 1,753
  • 1
  • 16
  • 26
  • This doesn't working when using this plugin: https://github.com/katzer/cordova-plugin-background-mode . It used to work without. I didn't find any solution on the plugin's github repo, maybe you have some fix for this? – Raz Buchnik Oct 30 '19 at 15:47
3

You can use window.onunload event.

Work for me just fine.

Nick
  • 95
  • 8
1

I don't think you can catch the user killing your app in the task manager at the time they do it. That's kind of the point of the task manager. However if people use that to cheat in the game you could set a flag in memory to say they are in the game and have the program unset it it they exit normally. If they start the game with the in the game flag still set you'd know they had cheated and could trigger the looseLife() thing?

Tim 333
  • 942
  • 1
  • 8
  • 9
1

Use the beforeunload event:

window.addEventListener('beforeunload', function(event) {
  console.log(`do something with the event:`, event);
  // most recommended is to emit socket like this (just for example):
  // socket.emit("exit-app", {access_token});
});

This should be emitted and run just before app is getting closed. The most recommended way to do things with this method is emit to server using sockets (Socket.io for example). Don't use HTTP request since HTTP request waits for callbacks or any handshake while sockets no. In this case using sockets is preferable.

Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96