Crash with back button

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.

One problem with my workaround is that it is now leaking a ServiceConnection. (Intent { act=com.google.android.gms.ads.identifier.service.START pkg=com.google.android.gms })

2019-01-01 13:22:46.394 25071-25071/org.sralab.emgimu E/ActivityThread: Activity org.sralab.emgimu.powerhammer.PowerHammerActivity has leaked ServiceConnection null that was originally bound here
    android.app.ServiceConnectionLeaked: Activity org.sralab.emgimu.powerhammer.PowerHammerActivity has leaked ServiceConnection null that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1610)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1502)
        at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1659)
        at android.app.ContextImpl.bindService(ContextImpl.java:1612)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:698)
        at org.sralab.emgimu.powerhammer.PowerHammerActivity.bindService(PowerHammerActivity.java:63)
        at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
        at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
        at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source:72)
        at android.os.MessageQueue.next(MessageQueue.java:395)
        at android.os.Looper.loop(Looper.java:160)
        at com.unity3d.player.UnityPlayer$e.run(Unknown Source:32)

I’m guessing this must be unbound by onPause. This also doesn’t seem to happen unless I request binding my service. Either way, I filter out this intent and don’t forward it to bindService and things seem to work.

Hi! Did you solve this or found where the problem was? We are having lots and lots of crashes like this on our game on Android 9 (API 28). Thanx!

bump

EDIT: We are having this problem whenever we finish the activity and if UnityPlayer is added to the view hierarchy, if we add UnityPlayer to anywhere inside the layout, or even remove it a few seconds before closing the activity, the crash occurs

EDIT: Also only on API 28, tested on 2018.3.6 and still no change

Im getting hundreds of these crashes in production… in fact I dont know if it’s this same crash or not, but it might be related: Unity Issue Tracker - [Android] Application.Quit and Application.Unload crashes the app on Android 9.x devices when Scripting backend is set to IL2CPP

I just upgraded from 2018.3.2 to 2018.3.12 and we also started getting 100s of crashes from Users trying to exit the app. It crashed for myself too and could see the error in Logcat.

Rolled back to 2018.3.2 and its back to normal, no crashing.

Also strugglin with this error. Ver 2018.3.12

Any ideas?

No solution yet? I don’t know what to do anymore. Still happening on 2019.1.2

Any solution?

Any solution I am also facing the same crash.

I think this is the same issue:
Native method not found: com.unity3d.player.UnityPlayer.nativeRestartActivityIndicator:()V page-4#post-4990409

They are fixing it