- Home /
The question is answered, right answer was accepted
Android: 'back' button event
Hello!
Is there any way to know when 'back' button on the phone is pressed? I'd like to exit the game when this button is pressed, and not hide it (if hidden, after touching the icon the second time - the game resumes from the previous point and does not restart).
thanks
Word on the forums is that we'll be able to handle the back button as the escape keycode in the upco$$anonymous$$g RC2.
Answer by RitchParker · Oct 28, 2010 at 04:08 AM
The "escape" key label appears to be mapped to the back button on the android platform in the final release of Unity 3.0. As far as other mappings I've found that the "menu" key label is mapped to the menu key and that that MonoBehaviours will get an "OnApplicationPause" + true call when the home button is pressed and an "OnApplicationPause" + false when the application is brought back as the current process. I've not found a mapping for the search button as of yet.
Could you also post the code required to make the game run in the background after the back button is pressed ? thanks.
You can't run an application in the background on Android.
thanks for replying, I want my game to resume after its relaunched from the home screen in android, basically imitating the behaviour of the app when the Home button is pressed.
I believe as of now (Unity 5.5/5.6) you can run apps in the background in Android. At least Hearthstone found a way to do it.
it doesn't run but stays suspended in the background
Thank you VERY VERY $$anonymous$$uch RitchParker, this helped me a lot in creating my android game which needs to be on the pause screen when the user presses the Back $$anonymous$$ey or $$anonymous$$enu $$anonymous$$ey.
Answer by lbalasubbaiahapps · Sep 30, 2011 at 02:55 PM
write this your scripts (KeyCode.Escape is code for native "Back" button on Android):
if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); }
No Doubt it will work try this one.i tried also.
but that closes the app, how to set it to go to the previous scene?
Please don't add new questions as comments to 3yr old threads. But the answer is simple: replace Application.Quit();
with Application.LoadLevel("YourPreviousLevel");
Hi, i am too new to unity. $$anonymous$$ay i know where should i add these lines?
hi,,balasubbaiahapps !!! it work. your code helpful. tank you very much..!
Hi, i am too new to unity. $$anonymous$$ay i know where should i add these lines?
you should add this code in void Update() method.!
I know this is closed, however It would be nice if when testying with Unity Remote the back button would be recognised.
this will make the app quit completely,how to resume the app after its reopened?
to do that you need to save the current game stats to a list/array and then on loading the game back again check for this file and load from it.
Follow this Question
Related Questions
How can I ensure my android app will run on 2.1 (Eclair)? 1 Answer
Android: prevent phone from sleeping 2 Answers
AsyncOperation.progress still broken? 4 Answers
Unity 3 Android license question 2 Answers
Unity Android Market Publishing Problem 4 Answers