Search Unity

Android's button "Back"

Discussion in 'Android' started by itanium, Nov 16, 2014.

  1. itanium

    itanium

    Joined:
    Feb 23, 2012
    Posts:
    20
    Hello,
    I've read many topics about Back button on Android, but none of there mentioned solutions worked for me.
    Android 4.4.2

    Can anyone please share with a working solution, that captures some value (1 or true), when Back button is being pressed and not a frame longer?

    What I've tried:
    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.Escape))
    and
    Code (CSharp):
    1. if(Input.GetKey(KeyCode.Escape))
    Works great in Unity Editor, doesn't work on phone (no reaction)

    Then in InputManager's Axes I created a custom axes called Escape with Positive Button = escape
    Afterwards I've tried:
    Code (CSharp):
    1. if(Input.GetButton("Escape"))
    Works great in Unity Editor, but on phone captures a lot more than a single click - because of
    ---
    Code (CSharp):
    1. if(Input.GetButtonDown ("Escape"))
    Works great in Unity Editor, but doesn't work on phone (no reaction)

    Last one - created a variable PrevAxis, which stores GetAxis("Escape") value from previous frame:
    Code (CSharp):
    1. private float PrevAxis=0;
    then in Update() added lines, that check, if Escape is pressed and was it pressed in a previous frame:
    Code (CSharp):
    1. if((Input.GetAxis ("Escape")==1)&&(PrevAxis==0))
    2. {
    3. }
    4. PrevAxis = Input.GetAxis ("Escape");
    And again : works great in Unity Editor, but on phone doesn't work at all (no reaction)

    P.S. With "no reaction" I mean - no reaction in my game, because when I press Back button, my phone vibrates as it should.
     
    Last edited: Nov 16, 2014
  2. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    Hmmm, not sure I have an answer for you, but I can confirm that we use "Input.GetKeyDown(KeyCode.Escape)" in all our games and it works fine. Just tested on our device running 4.4.4 and it works.

    Our code is pretty simple: in Update(), we just check Input.GetKeyDown(KeyCode.Escape) and then do any processing as a result.
     
  3. itanium

    itanium

    Joined:
    Feb 23, 2012
    Posts:
    20
    Interesting, I'm doing exactly the same, but once again : in Unity Editor everything works fine (on keyboard's Esc), but on phone - just vibrates and does nothing.

    Tried on two different phones (4.2 and 4.4). Both phones have sensor buttons below screen.

    Code (CSharp):
    1. void Update ()
    2. {
    3.     if(Input.GetKeyDown(KeyCode.Escape))
    4.     {
    5.         //Do something!
    6.     }
    7. }
    Any ideas?
     
    Last edited: Nov 19, 2014
  4. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    What are you trying to do when the back button is pressed?

    Are you using Debug.Log to log its actually being pressed and looking at the logs as its happening?
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    can confirm works fine for me using escape
     
  6. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    No good ideas here unfortunately, but I can confirm that it works fine for me as well... Android 4.4.4

    Perhaps try a blank project with a simple action to see if it works in that case?
     
  7. Bendstrup1996

    Bendstrup1996

    Joined:
    Jun 21, 2018
    Posts:
    4
    1. if (Application.platform == RuntimePlatform.Android)
    2. {
    3. if (Input.GetKey(KeyCode.Escape))
    4. {
    5. // Insert Code Here (I.E. Load Scene, Etc)
    6. // OR Application.Quit();

    7. return;
    8. }