androidunity-game-engineandroid-manifestandroid-unity-plugin

My app crashes while changing screen orientation of unity player


I've my android app which opens unity player on click. My unity player performs as expected.

Issue: On changing screen rotation, app crashes.

Debug Console Output:

I/Unity: StopVuforia

         (Filename: ./Runtime/Export/Debug.bindings.h Line: 43)
D/AR: RenderManager: Could not retrieve a valid GLSurfaceView in view hierarchy, therefore cannot set any render mode
W/MessageQueue: Handler (android.os.Handler) {90dbee5} sending message to a Handler on a dead thread
                java.lang.IllegalStateException: Handler (android.os.Handler) {90dbee5} sending message to a Handler on a dead thread
                    at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
                    at android.os.Handler.enqueueMessage(Handler.java:643)
                    at android.os.Handler.sendMessageAtTime(Handler.java:612)
                    at android.os.Handler.sendMessageDelayed(Handler.java:582)
                    at android.os.Handler.sendMessage(Handler.java:519)
                    at android.os.Message.sendToTarget(Message.java:416)
                    at com.unity3d.player.UnityPlayer$e.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer$e.b(Unknown Source)
                    at com.unity3d.player.UnityPlayer.b(Unknown Source)
                    at com.unity3d.player.UnityPlayer.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer$17.surfaceDestroyed(Unknown Source)
                    at android.view.SurfaceView.updateWindow(SurfaceView.java:588)
                    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:247)
                    at android.view.View.dispatchDetachedFromWindow(View.java:15567)
                    at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:4914)
                    at android.view.ViewGroup.removeAllViews(ViewGroup.java:4860)
                    at com.unity3d.player.UnityPlayer.quit(Unknown Source)
                    at com.Accenture.Activity.UnityPlayerActivity.onDestroy(UnityPlayerActivity.java:66)
                    at android.app.Activity.performDestroy(Activity.java:6889)
                    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1175)
                    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4231)
                    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4262)
                    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4536)
                    at android.app.ActivityThread.-wrap19(ActivityThread.java)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:154)
                    at android.app.ActivityThread.main(ActivityThread.java:6165)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
W/MessageQueue: Handler (android.os.Handler) {90dbee5} sending message to a Handler on a dead thread
                java.lang.IllegalStateException: Handler (android.os.Handler) {90dbee5} sending message to a Handler on a dead thread
                    at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
                    at android.os.Handler.enqueueMessage(Handler.java:643)
                    at android.os.Handler.sendMessageAtTime(Handler.java:612)
                    at android.os.Handler.sendMessageDelayed(Handler.java:582)
                    at android.os.Handler.sendMessage(Handler.java:519)
                    at android.os.Message.sendToTarget(Message.java:416)
                    at com.unity3d.player.UnityPlayer$e.b(Unknown Source)
                    at com.unity3d.player.UnityPlayer.b(Unknown Source)
                    at com.unity3d.player.UnityPlayer.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer.a(Unknown Source)
                    at com.unity3d.player.UnityPlayer$17.surfaceDestroyed(Unknown Source)
                    at android.view.SurfaceView.updateWindow(SurfaceView.java:588)
                    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:247)
                    at android.view.View.dispatchDetachedFromWindow(View.java:15567)
                    at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:4914)
                    at android.view.ViewGroup.removeAllViews(ViewGroup.java:4860)
                    at com.unity3d.player.UnityPlayer.quit(Unknown Source)
                    at com.Accenture.Activity.UnityPlayerActivity.onDestroy(UnityPlayerActivity.java:66)
                    at android.app.Activity.performDestroy(Activity.java:6889)
                    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1175)
                    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4231)
                    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4262)
                    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4536)
                    at android.app.ActivityThread.-wrap19(ActivityThread.java)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:154)
                    at android.app.ActivityThread.main(ActivityThread.java:6165)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
W/Unity: Timeout while trying detaching primary window.

AndroidManifest

<activity
            android:name=".UnityPlayerActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:theme="@style/UnityThemeSelector">
            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
        </activity>

Workarounds:

Either "fixing of screen changing app crash" or "force landscape view" will make my resolution.

  1. I've tried changing unity player setting to landscape-left.
  2. I've tried removing my orientation|layoutDirection|screenLayout from android manifest file.
  3. I've tried adding screenOrientation="landscape/user/nosensor/fulluser"

Solution

  • I had googled a lot for Unity Player crash while changing orientation, I didn't found a useful solution.

    Therefore, as it was my feasibility, I had locked my orientation to "landscape" mode only.

    For this,

    Step 1: Unity Player settings

    Go to Edit > Project Settings > Player it will open Player Settings in the inspector.

    Select the target platform from the list.

    Click the Resolution and Presentation section.

    Set the Default Orientation to Landscape left / right.

    Refer this blog: https://answers.unity.com/questions/774186/landscape-mode-only.html

    Step 2: Android Code Changes.

    In manifest file,

    <activity
                android:name=".UnityPlayerActivity"
                android:label="@string/app_name"
                android:launchMode="singleTask"
                android:screenOrientation="landscape"
                android:theme="@style/UnityThemeSelector">
                <meta-data
                    android:name="unityplayer.UnityActivity"
                    android:value="true" />
    </activity>
    

    Also, make the orientation programmatically as well.

    private int screenOrientation;
    // lock screen orientation to landscape mode only.
    screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    setRequestedOrientation(screenOrientation);