Android 8.0 Back button?

I’m creating my first Android App. I’m trying to capture the back button tap to load a previous scene, but it’s not working.

I’m testing with a Pixel 2 using the newest Android 8.0.0

I’ve tested via Unity Remote and via a full build to the device.

I’m trying to capture the back button with Input.GetKeyDown(Keycode.Escape) like so:

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            SceneManager.LoadScene("StartScene");
        }
    }

This just isn’t working. GetKeyDown is never called for that KeyCode. How do I capture the Back Button on Android 8?

Thanks all.

Chris

You are using SceneManager, that requires namespace

using UnityEngine.SceneManagement;

on the top of your code to be executed. Insert this line to make it execute. I recommend going through this documentation: Unity - Manual: Work with multiple scenes in Unity
(Are you sure your current Unity version supports the SceneManager namespace? It was added around Unity 5, so earlier versions cannot handle this namespace for obvious reasons).

The second problem could be, that you are referencing a wrong scene. Check the following:

  • is your scene name properly enter (ie. StartScene is not equals to startscene), check for misspelling,
  • is your scene added to the game? Check your File / Build settings / Scenes in build (Unity - Manual: Build Settings). If your referenced scene is not showing up here, Unity will not see it, so you must add it with drag and drop.

The third problem could be, that you have not attached the script to any GameObject so Unity will not bother executing it, or you have added this line to a GameObject that doesn’t exist yet.

Anyways the code seems to be working.

Thank you for your reply and suggestions Kispo.

  1. I am of course using the UnityEngine.SceneManagment include. I’m using Unity 5.6
  2. I’ve verified that my scene is correct and of course included in the Build.
  3. The script with this Update call is part of my Game Manager and is definitely attached to an active object.

I’ve included a Debug Statement now to catch if the back button is even registering, and it is not.

I wonder if this has to do with how on the Pixel 2 the Back Arrow is an on-screen touch button. You have to swipe up from the bottom to get it to display. This may be causing the app to lose focus and not catch the back button entries? I’m not sure But it’s definitely not working as is.