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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Back button while loading a scene closes the app

Discussion in 'Android' started by AndreaP, Dec 27, 2013.

  1. AndreaP

    AndreaP

    Joined:
    Dec 28, 2012
    Posts:
    50
    Hello,
    I'm currently using Unity 4.3.1 free and I'm experiencing this unwanted behaviour on Android:

    if I press the back button at the right moment while a scene is loading the application quits.
    Scripts of the new scene reach the Start() and Update(), but before anything appears in the screen the game just closes.
    I can't find anything on my scripts that would do that, so I'm starting to think it could be an Unity issue.

    Has anyone else encountered the same thing? I tested this on an S3 mini with Android 4.1.2 and a Nexus 7 with 4.4.2; it's easier to trigger on the S3 mini because the loading times are longer but it can happen on the N7 too.
     
    Last edited: Dec 27, 2013
  2. AndreaP

    AndreaP

    Joined:
    Dec 28, 2012
    Posts:
    50
    I created a very simple test application with two scenes. Each scene contains just some big GUITexture to increase the loading time. There is only a script that on any keypress loads the other scene. If I repeadetly press the back button while it's loading the application closes. I'm pretty sure it's an Unity bug now.
     
  3. jvil

    jvil

    Joined:
    Jul 13, 2012
    Posts:
    263
    This is not a bug, is the intended behaviour on Android apps. You need to override the back button, can do it via native Android plugin or via Unity with Input.GetKeyDown.

    Example:

    Code (csharp):
    1. void Update()
    2. {
    3.    if (Input.GetKeyDown(KeyCode.Escape))
    4.    {
    5.         return;
    6.    }
    7. }
    Check which KeyCode is the equivalent to Android back button, I don't remember if was KeyCode.Escape or some other.
     
  4. AndreaP

    AndreaP

    Joined:
    Dec 28, 2012
    Posts:
    50
    Yes, it's Escape and I already use that. The game does not close if I press the back button after the loading is completed.
    There seems to be a short time window during the loading of a new scene where the behaviour I described happens (the application closes itself).
     
  5. jvil

    jvil

    Joined:
    Jul 13, 2012
    Posts:
    263
    That's probably because the gameObject that's checking Input.GetKeyDown is destroyed when changing scene, you would need to add DontDestroyOnLoad on this gameObject or do it via Android plugin.
     
  6. AndreaP

    AndreaP

    Joined:
    Dec 28, 2012
    Posts:
    50
    Just tried adding a DontDestroyOnLoad script on the first scene and the problem is still there.
    Maybe an Android plugin could solve the issue but I'll try only if nothing else works.
    Thanks for the replies anyway :)
     
  7. DATuan91

    DATuan91

    Joined:
    Aug 2, 2013
    Posts:
    9
    Did you find out solutions? I have the same issue, and can not fix either