Android: black screen after lock and unlock / application pause

Hello folks,

I have been struggling with an issue (Unity 2017.1.2) and found a work around, I thought I might share it and maybe hear of a cleaner solution here.

Symptoms:

  • release build (development is fine) on android
  • open the app
  • lock the screen
  • wait for 30 seconds or so
  • unlock it: black screen with the audio still runing and the interface responding.

Adb only reports Screen frozen for +xxx ms due to Window {xxxxx com.unity3d.player.UnityPlayerNativeActivity}
Issue is quite similar to this bug on Unreal: https://answers.unrealengine.com/questions/187022/android-freeze-game-in-background.html

When the application is open during screenlock, it actually pauses (and do not loose focus) and OnResume ( OnApplicationPause(false) ) is called when you unlock. It seems that Unity isn’t redrawing the app by itself then, no matter how long you wait. It seems it has to do with the screen orientation changing during the lock.

For now, I strangely found a fix in the form of:

    private void OnApplicationPause(bool pause)
    {
        if(!pause)
        {
#if UNITY_ANDROID
            Canvas.ForceUpdateCanvases();
            Debug.Log("force redraw !");
#endif
        }
    }

I hope that might help someone and I would be very pleased to hear of a cleaner fix.

2 Likes

Not a knowledgeable Unity person but I’d bet that the issue is related to the loss of the GLES device context. If you search theres stuff about rendertextures needing to be redrawn after locking on Android due to context loss.

1 Like