androidandroid-activityrecreate

how to check if android going to recreate activity or destroy?


I have an android studio project. When I am rotating screen, android destroys and recreates main activity. How can I check during the destruction, if android going to recreate activity?


Solution

  • You can determine if the activity is finishing by user choice (user chooses to exit by pressing back for example) using isFinishing() in onDestroy.

      @Override
      protected void onDestroy() {
        super.onDestroy();
        if (isFinishing()) {
          // wrap stuff up
        } else { 
          //It's an orientation change.
        }
      }
    

    Another alternative (if you're only targeting API>=11) is isChangingConfigurations.

      @Override
      protected void onDestroy() {
        super.onDestroy();
        if (isChangingConfigurations()) {
          //It's an orientation change.
        }
      }