Try to work around Android's handling of static variables in terminated apps
authorGabriel Jacobo <gabomdq@gmail.com>
Wed, 12 Feb 2014 18:12:14 -0300
changeset 820956e9c709db7e
parent 8208 e749005cfd72
child 8210 581caa876b2e
Try to work around Android's handling of static variables in terminated apps

Android, we want to love you, but you don't make it easy for us...
android-project/src/org/libsdl/app/SDLActivity.java
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Feb 10 13:40:02 2014 -0800
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Wed Feb 12 18:12:14 2014 -0300
     1.3 @@ -28,7 +28,7 @@
     1.4      private static final String TAG = "SDL";
     1.5  
     1.6      // Keep track of the paused state
     1.7 -    public static boolean mIsPaused = false, mIsSurfaceReady = false, mHasFocus = true;
     1.8 +    public static boolean mIsPaused, mIsSurfaceReady, mHasFocus;
     1.9      public static boolean mExitCalledFromJava;
    1.10  
    1.11      // Main components
    1.12 @@ -53,22 +53,37 @@
    1.13          //System.loadLibrary("SDL2_ttf");
    1.14          System.loadLibrary("main");
    1.15      }
    1.16 +    
    1.17 +    
    1.18 +    public static void initialize() {
    1.19 +        // The static nature of the singleton and Android quirkyness force us to initialize everything here
    1.20 +        // Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
    1.21 +        mSingleton = null;
    1.22 +        mSurface = null;
    1.23 +        mTextEdit = null;
    1.24 +        mLayout = null;
    1.25 +        mJoystickHandler = null;
    1.26 +        mSDLThread = null;
    1.27 +        mAudioTrack = null;
    1.28 +        mExitCalledFromJava = false;
    1.29 +        mIsPaused = false;
    1.30 +        mIsSurfaceReady = false;
    1.31 +        mHasFocus = true;
    1.32 +    }
    1.33  
    1.34      // Setup
    1.35      @Override
    1.36      protected void onCreate(Bundle savedInstanceState) {
    1.37 -        //Log.v("SDL", "onCreate()");
    1.38 +        Log.v("SDL", "onCreate():" + mSingleton);
    1.39          super.onCreate(savedInstanceState);
    1.40          
    1.41 +        SDLActivity.initialize();
    1.42          // So we can call stuff from static callbacks
    1.43          mSingleton = this;
    1.44  
    1.45          // Set up the surface
    1.46          mSurface = new SDLSurface(getApplication());
    1.47          
    1.48 -        // Make sure this variable is initialized here!
    1.49 -        mExitCalledFromJava = false;
    1.50 -        
    1.51          if(Build.VERSION.SDK_INT >= 12) {
    1.52              mJoystickHandler = new SDLJoystickHandler_API12();
    1.53          }
    1.54 @@ -118,7 +133,6 @@
    1.55  
    1.56      @Override
    1.57      protected void onDestroy() {
    1.58 -        super.onDestroy();
    1.59          Log.v("SDL", "onDestroy()");
    1.60          // Send a quit message to the application
    1.61          SDLActivity.mExitCalledFromJava = true;
    1.62 @@ -135,6 +149,10 @@
    1.63  
    1.64              //Log.v("SDL", "Finished waiting for SDL thread");
    1.65          }
    1.66 +            
    1.67 +        super.onDestroy();
    1.68 +        // Reset everything in case the user re opens the app
    1.69 +        SDLActivity.initialize();
    1.70      }
    1.71  
    1.72      @Override