Android: fix IllegalStateException in onBackPressed()
authorSylvain Becker <sylvain.becker@gmail.com>
Fri, 20 Dec 2019 15:58:59 +0100
changeset 133681219018fa220
parent 13367 aa6c25160e67
child 13369 d73580c86f81
Android: fix IllegalStateException in onBackPressed()

Rare exception, not catch-able, which appears when the activity gets in a broken
state.

java.lang.IllegalStateException:
at android.app.FragmentManagerImpl.checkStateLoss (FragmentManagerImpl.java:1323)
at android.app.FragmentManagerImpl.popBackStackImmediate (FragmentManagerImpl.java:493)
at android.app.Activity.onBackPressed (Activity.java:2215)
at org.libsdl.app.SDLActivity.onBackPressed (SDLActivity.java)
at android.app.Activity.onKeyUp (Activity.java:2193)
at android.view.KeyEvent.dispatch (KeyEvent.java:2685)
at android.app.Activity.dispatchKeyEvent (Activity.java:2423)
at org.libsdl.app.SDLActivity.dispatchKeyEvent (SDLActivity.java)
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Fri Dec 20 14:33:37 2019 +0300
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Fri Dec 20 15:58:59 2019 +0100
     1.3 @@ -453,7 +453,9 @@
     1.4          }
     1.5  
     1.6          // Default system back button behavior.
     1.7 -        super.onBackPressed();
     1.8 +        if (!isFinishing()) {
     1.9 +            super.onBackPressed();
    1.10 +        }
    1.11      }
    1.12  
    1.13      // Called by JNI from SDL.
    1.14 @@ -466,7 +468,9 @@
    1.15          runOnUiThread(new Runnable() {
    1.16              @Override
    1.17              public void run() {
    1.18 -                SDLActivity.this.superOnBackPressed();
    1.19 +                if (!SDLActivity.this.isFinishing()) {
    1.20 +                    SDLActivity.this.superOnBackPressed();
    1.21 +                }
    1.22              }
    1.23          });
    1.24      }