Search Unity

Crash with back button

Discussion in 'Android' started by peabody124, Jan 1, 2019.

  1. peabody124

    peabody124

    Joined:
    Nov 3, 2018
    Posts:
    2
    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:

    Code (JavaScript):
    1.     @Override
    2.     public void onBackPressed() {
    3.         exiting = true;
    4.         super.onBackPressed();
    5.     }
    6.  
    7.     @Override
    8.     public boolean onKeyDown(int keyCode, KeyEvent event) {
    9.         if ((keyCode == KeyEvent.KEYCODE_BACK)) {
    10.             onBackPressed();
    11.             return true;
    12.         }
    13.         return super.onKeyDown(keyCode, event);
    14.     }
    15.  
    And what I found was this crash:
    Code (JavaScript):
    1. 2019-01-01 11:57:26.858 31376-31376/org.sralab.emgimu E/AndroidRuntime: FATAL EXCEPTION: main
    2.     Process: org.sralab.emgimu, PID: 31376
    3.     java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.WindowInsets.isConsumed()' on a null object reference
    4.         at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7077)
    5.         at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1661)
    6.         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2074)
    7.         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
    8.         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
    9.         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
    10.         at android.view.Choreographer.doCallbacks(Choreographer.java:761)
    11.         at android.view.Choreographer.doFrame(Choreographer.java:696)
    12.         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
    13.         at android.os.Handler.handleCallback(Handler.java:873)
    14.         at android.os.Handler.dispatchMessage(Handler.java:99)
    15.         at android.os.Looper.loop(Looper.java:193)
    16.         at android.app.ActivityThread.main(ActivityThread.java:6680)
    17.         at java.lang.reflect.Method.invoke(Native Method)
    18.         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    19.         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

    Code (JavaScript):
    1.     protected void safePause()
    2.     {
    3.         super.onPause();
    4.     }
    5.  
    6.     // Pause Unity
    7.     @Override protected void onPause()
    8.     {
    9.         super.onPause();
    10.         mUnityPlayer.pause();
    11.     }
    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.
     
  2. peabody124

    peabody124

    Joined:
    Nov 3, 2018
    Posts:
    2
    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 })

    Code (JavaScript):
    1. 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
    2.     android.app.ServiceConnectionLeaked: Activity org.sralab.emgimu.powerhammer.PowerHammerActivity has leaked ServiceConnection null that was originally bound here
    3.         at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1610)
    4.         at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1502)
    5.         at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1659)
    6.         at android.app.ContextImpl.bindService(ContextImpl.java:1612)
    7.         at android.content.ContextWrapper.bindService(ContextWrapper.java:698)
    8.         at org.sralab.emgimu.powerhammer.PowerHammerActivity.bindService(PowerHammerActivity.java:63)
    9.         at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    10.         at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
    11.         at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source:72)
    12.         at android.os.MessageQueue.next(MessageQueue.java:395)
    13.         at android.os.Looper.loop(Looper.java:160)
    14.         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.
     
  3. Zeeppo-Games

    Zeeppo-Games

    Joined:
    May 13, 2015
    Posts:
    102
    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!
     
  4. badjano

    badjano

    Joined:
    Aug 2, 2012
    Posts:
    24
    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
     
    Last edited: Feb 25, 2019
  5. pertz

    pertz

    Joined:
    Jan 8, 2015
    Posts:
    106
  6. MochiBits

    MochiBits

    Joined:
    Mar 11, 2014
    Posts:
    30
    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.
     
  7. Neozman

    Neozman

    Joined:
    Sep 29, 2016
    Posts:
    67
    Also strugglin with this error. Ver 2018.3.12
     
  8. Neozman

    Neozman

    Joined:
    Sep 29, 2016
    Posts:
    67
    Any ideas?
     
  9. Zeeppo-Games

    Zeeppo-Games

    Joined:
    May 13, 2015
    Posts:
    102
    No solution yet? I don't know what to do anymore. Still happening on 2019.1.2
     
    Last edited: May 27, 2019
  10. achimeno

    achimeno

    Joined:
    Nov 13, 2014
    Posts:
    16
    Any solution?
     
  11. unity_xrPBQ-2zcfd-DA

    unity_xrPBQ-2zcfd-DA

    Joined:
    Jun 15, 2018
    Posts:
    19
    Any solution I am also facing the same crash.
     
  12. Zeeppo-Games

    Zeeppo-Games

    Joined:
    May 13, 2015
    Posts:
    102