5

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?

Vahag Chakhoyan
  • 873
  • 1
  • 10
  • 21
  • On orientation change, Android always destroys your activity and then recreates it. – Taseer Jul 14 '19 at 10:31
  • why do you want to know the system is going to recreate your activity? – Lai Lee Jul 14 '19 at 10:31
  • if you don't want system destroy and then recreate your activity while rotating, I think you could refer this solution https://stackoverflow.com/a/7618926/3728335 – Lai Lee Jul 14 '19 at 10:43

2 Answers2

8

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.
    }
  }
Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24
  • 1
    What if the Android decided to kill the app due to memory constraints or the app crashed? In that case, `onDestroy` will **not** be called. Coding in a way that solves problems without undertaking worst-case scenarios is not a way to go. – Taseer Jul 14 '19 at 10:37
  • True, MVVM is the way to go. But a question demands an answer. – Chrisvin Jem Jul 14 '19 at 10:40
  • @TaseerAhmad But I've always found that particular reason unsatisfactory, crashes/force closes are going to mess up your expected code (atleast for basic stuff such as expectant handling of orientation changes) regardless of what design practice you follow. – Chrisvin Jem Jul 14 '19 at 10:44
  • you might want to check regarding `noHistory=true` or ` `Intent.FLAG_ACTIVITY_NO_HISTORY`. See here for details: https://stackoverflow.com/a/62107555/3763032 and yes, MVVM is preferred to handle /save complex data – mochadwi May 30 '20 at 19:22
  • Even the ViewModel will be cleared if the activity Is recreated due to Memory Constraints Right? – Rissmon Suresh Aug 20 '21 at 17:10
1

Override the Activity lifecycle methods to see the flow.And then use the appropriate method to check activity current state like isChangingConfigurations() Example code snippet.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(MainActivity.class.getSimpleName(),"OnStart Called");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(MainActivity.class.getSimpleName(),"OnRestart Called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(MainActivity.class.getSimpleName(),"OnDestroy Called");
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.i(MainActivity.class.getSimpleName(),"OnPause Called");
    }

   @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i(MainActivity.class.getSimpleName(),"OnConfiguration Changed Called");
    }

}

For more details see the official page activity-lifecycle

CodeWithVikas
  • 1,105
  • 9
  • 33