How to catch exit app event?
Asked Answered
J

4

11

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?

Jaggy answered 1/5, 2014 at 14:18 Comment(8)
#18932222Liv
@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!Jaggy
Anybody? Please Its very important for me! I can't imagine there's no event or workaround for this issue.Jaggy
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.Prebo
@Prebo 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...Jaggy
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.Prebo
@Prebo 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...Jaggy
It will work if you implement it in a plugin I guess.Prebo
U
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.

Uund answered 30/11, 2015 at 22:23 Comment(1)
This doesn't working when using this plugin: 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?Tattletale
A
3

You can use window.onunload event.

Work for me just fine.

Ashien answered 15/10, 2015 at 14:4 Comment(0)
H
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?

Halve answered 3/6, 2014 at 23:15 Comment(0)
T
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.

Tattletale answered 15/10, 2019 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.