I’m running my unity application as one activity within a number of them, so want the back button to work and go through the back stack. I’m overriding UnityPlayerActivity (primarily to hook into a service related to my application). I made the code hook the back button by overriding the default onKeyDown:
@Override
public void onBackPressed() {
exiting = true;
super.onBackPressed();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
onBackPressed();
return true;
}
return super.onKeyDown(keyCode, event);
}
And what I found was this crash:
2019-01-01 11:57:26.858 31376-31376/org.sralab.emgimu E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.sralab.emgimu, PID: 31376
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.WindowInsets.isConsumed()' on a null object reference
at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7077)
at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1661)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2074)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
This crash is dependent on the call to mUnityPlayer.onPause() in UnityPlayerActivity. Unfortunately I cannot find a workaround that doesn’t break encapsulation so I had to implement
protected void safePause()
{
super.onPause();
}
// Pause Unity
@Override protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
}
In UnityPlayerActivity. I’m not sure if there is something more clever I should be doing to prepare the player for the close, but this seems to reflect a bug in Unity as far as I can tell. This is with 2018.3.02f on OSX compiling via Gradle compiling with API 28.