Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can I know if a user has closed my app from "home page button" on his android device ?

Discussion in 'Scripting' started by Essotaha, Apr 14, 2021.

  1. Essotaha

    Essotaha

    Joined:
    Aug 15, 2020
    Posts:
    3
    Hello Unity's members ..

    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 !

    I hope you help me ...thank you all :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    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.
     
  3. Essotaha

    Essotaha

    Joined:
    Aug 15, 2020
    Posts:
    3
    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.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    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.
     
  5. Essotaha

    Essotaha

    Joined:
    Aug 15, 2020
    Posts:
    3
    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" ?
     
  6. SIFORGAMES

    SIFORGAMES

    Joined:
    Nov 5, 2020
    Posts:
    1
    Hi did you manage to sort out the issue ?
    we have been going through the same issue for a bit now.... I would appreciate any help with it.
     
  7. PvR2018

    PvR2018

    Joined:
    Jul 16, 2022
    Posts:
    1
    I also Have this problem.
    Anyone of you!! find any way to do this??
     
    kkfrank1 likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Please don't necro-post. When you have a problem, make your own post. It's FREE!

    When you post, remember we cannot read your mind. YOU must communicate. Here's how:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/