How can I make my app close properly?

I created a simple bouncy ball game and published it to the play store. When you tap, a ball gets an impulse up and will fall down with gravity to the floor.

When testing it, I noticed that when I hit the home button the app does not close properly. It still has processes running in the background. I did not add menus of any sort to the game.

I thought the compiler would add the home button functionality for me, where if you press it and quit and game closes properly.

Do I need to add this myself? If so, how?

As far as I understand mobile operating systems, pressing the Home button doesn’t quit apps. It suspends them. iOS and Android are both very good at managing app memory, and apps pushed to the background require almost no memory. If the OS does run into memory issues, it will shut down apps as needed, but otherwise, pressing Home, opening your browser and buying some shoes, then opening your app back up will just resume where it left off*. In almost all cases, this is what people want and expect.

If you really want your game to be shut down when the player switches to another app, Unity exposes the OnApplicationPause method. You could run Application.Quit() within that method, and your app will be killed. Note that this is not recommended behavior, and I’m actually not certain if it will cause issues with Apple’s review, if that is important to you.

Also, note that if you’re playing the game and you receive a phone call, this would cause your game to quit also, as the app is being pushed to the background in the same way it would be if you were to press Home.

    • You shouldn’t rely on the state being saved, as you’re not promised the OS won’t close the app when not running.

Application.Quit() is the right command. But as schneider points out, you shouldn’t have mobile games quit when the user presses the Home button, because neither Apple nor Google like that. You should quit the game when the user presses Back on Android; for that, respond to Input.GetKeyDown(KeyCode.Escape)

Thanks guys that’s exactly what I wanted to know