What I need to Know :
I need to know what is the script that I can check if a user has closed my app without clicking on “Quit” button that I created in my project !! for example : if a user has closed my app by home page button or maybe he switched off his android device ! in this case I can’t check if a user has closed my app or he hasn’t !!
What I tried :
I tried to calling code on OnApplicationFocus() or OnApplicationPause() functions but these functions check if focus is false or true only ! and they don’t guarantee that the user will exit the application ! and they will call code every time when get focus or pause true or false ! I mean if a user clicked on “Home Page button” I will get a “False” focus and if he return to the app window I will get “True” focus , So I can’t use it to check if user already closed my app !
I found on “Unity Documentation” the code (“Input.GetKey(KeyCode.Escape”) that calling “back” button on android device, But I need to know what is the code that check if a user click on button “Home Page” on his android device and close app window or if app got suspend or switched his mobile off !
Just use the OnApplicationFocus() and save your game state then. That will trip the moment the user touches the task bar, home button, back button, whatever, so you will get called before they even have a chance to swipe away. Same goes if he powers off, or if he holds the power button down for full-off dialog. The moment the dialog comes up, you’ll get called.
About the only thing the above won’t catch is if the battery gets yanked out or the phone hard-crashes.
thanks for reply …
Yup …I’m using this way already but I thought there another better way to check if user has closed my app or if my app stopped working …etc.
If your app locks up hard (like an infinite loop) you won’t get focus calls. Also, I think if it crashes to launcher / home you might not get it either. It might vary from platform to platform.
Either way, I’ve always used the above and I get consistent reliable results in iOS and Android all day long.
Well , What about firebase analytics ?
I want check if user closed my app because I want send an event to firebase analytics !
Onapplicationfocuse() is not help me for that!
I don’t understands why firebase analytics doesn’t have event “close_app” default like “remove_app” and “open_app” ?