Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Android Home button

Discussion in 'Android' started by Maklaud, Dec 13, 2014.

  1. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    Hi All!
    I really tried to google, nothing found.

    I need to process the phone Home button. I need to do some actions when players quit (works well) or minimize my game pressing the Home button. I read that the keyboard Home button doesn't work with this event.
    I also tried OnEnable, OnDisable, OnApplicationPause, OnApplicationFocus events. They work really strange (occur twice after pressing the Home button and then reopening the game).

    How else can I process that? I need to do some action when the player fully quits the game (it works) or quits it pressing the Home button (doesn't work). I need to do another action when the player starts the game (works well) or restores it after pressing the Home button (doesn't work).

    Thanks!
     
  2. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    Sounds like OnApplicationPause is what you are looking for. This doesn't map exactly one to one with user pressing the 'Home' button since the application can be paused for any number of reasons, but should be sufficient for most use cases. If that is not sufficient could you please explain a little more about what you are trying to do?

    Cheers
     
  3. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    I want to send Android notifications in a few days after the player quits the game. ApplicationQuit works well, but Home button doesn't. Though, it's rather common to quit the game like this - just press the Home button and then kill the game in the process manager.
    So, when the player presses the Home button, I want to set up an alarm, say, after 5 days. But if the player enters the game (after full restart or after minimizing the game) I want to clear that alarm so as not to bother the player.
     
  4. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    Okay, but OnApplicationPause should be enough to cover that use case.
     
  5. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    bitter, could you also explain to me the sequence of the events, please? It looks strange.

    I have different scenes and every scene's script extends my GameScreen script. In this GameScreen.cs I implement two methods - OnApplicationPause and OnApplicationFocus and write log in these methods. Then I start the game, press the Home phone button and then start the game again. And I see this sequence in the log - Pause, Focus, Focus, Pause. I totally don't understand this sequence of events.
     
  6. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    Yeah that sounds a bit weird. I would expect there to be at least three of each.
    Startup:
    OnApplicationPause(false)
    OnApplicationFocus(true)
    Home:
    OnApplicationFocus(false)
    OnApplicationPause(true)
    Resume:
    OnApplicationPause(false)
    OnApplicationFocus(true)

    I'm not 100% sure in which order OnApplicationPause and OnApplicationFocus are emitted but there should be at least on of each for each action (why I say at least one of each is because of the lock screen there can sometimes be multiple state changes before the application reaches a stable runnable state).
     
  7. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    bitter, thanks for you help! After using the events' parameters I think that's exactly what I need. Everytime I press the Home button, the Pause param is true, and when I restore the game, the Focus param is true. I may use it with the Quit and game start events.
    Thanks again!
     
  8. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    533
    It's strange, but looks like OnApplicationFocus(false) doesn't work on android.

    ... just tried OnApplicationPause doesn't work either.
    Does it work with home button and with switch apps button on android ?
     
  9. cdr9042

    cdr9042

    Joined:
    Apr 22, 2018
    Posts:
    162
    OnApplicationPause doesn't work for me. I use Google Admob and every time an ad is showed, OnApplicationPause get called. I only want an event when user pressed the Home button and leave the app