Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

back button & moveTaskToBack support

Discussion in 'Android' started by andyz, Aug 5, 2020.

  1. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,285
    You really need to support the back button in android but Unity doesn't give you simple functions to handle this, you kind of have to google how to handle it - there is no Android.OnBackButton or Android.MoveToBack for when on main menu.
    Would it be better to have some android-specific calls just to make code easier and more clear or does it just go against Unity's cross-platform API?

    Code (CSharp):
    1. //back button code:
    2. if (Input.GetKeyDown(KeyCode.Escape)) { }
    3.  
    4. //to send app into background (kinda awkward?)
    5. AndroidJavaObject activity = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" ).GetStatic<AndroidJavaObject>( "currentActivity" );
    6. activity.Call<bool>( "moveTaskToBack", true );
    7. activity.Dispose();
    8.