Question

was wondering if you could help. I'm under the impression that Android is not fully killing my application when ondestroy is called. This is because on the second launch I just end up getting a blank screen. I have read that this is common and you can call System.exit(0) in the ondestroy method (I know some people frown upon this), but I don't know what to do. Also, if I do have System.exit(0) in ondestroy my application wont open at all. Any ideas?

So application wont start with:

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    System.exit(0);
}

And this is my code from before that doesn't kill the application properly:

@Override
public void onDestroy() {
    super.onDestroy();
    //System.exit(0);
    try{
    Log.e("destroyed", "");
      int highSCORE = ResourcesManager.getInstance().getHighScore();

      SharedPreferences settings = getSharedPreferences(SHARED_PREFS_MAIN, 0);
      SharedPreferences.Editor editor = settings.edit();
      if (highSCORE > highScore)
        {
            editor.putInt("highest", highSCORE);
        }

        editor.putBoolean("music", ResourcesManager.getInstance().isMusicAllowed());
        editor.putBoolean("sound", ResourcesManager.getInstance().isSoundAllowed());
      // Commit the edits!
      editor.commit();

    // Destroy the AdView.
    if (adView != null) {
        adView.destroy();
    }
    }catch(Exception e){

    }
}

EDIT: I'm really confused by this, but it seems that onDestroy is being called as soon as the application is created, which obviously contradicts the lifecycle. I have found a work around by placing a check in onDestroy to check if the game has been loaded, if it has then kill the app. This was found on this site: http://www.matim-dev.com/handling-onresume-onpause-and-ondestroy.html. Does anyone know why this happened? Thanks.

Was it helpful?

Solution

See comments above. Call custom code prior to calling the super's onDestroy(). In fact, I do this with all the "exit" functions - pause, stop, and destroy. I do this to ensure the code is executed before the super does what it needs to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top