How to distinguish between orientation change and leaving application android

I understand that when the orientation of the screen changes, the current activities onDestroy() is called followed by the onCreate() to effectively recreate the activity. I need to know how to programmatically tell if the application is being exited or if just the orientation is being changed.

One method is for the previous activity to notify me when its onResume() method is being called, this will let me know that the user has pressed the back button and the orientation hasn't been changed.

P.S. I'm looking for a solution more elegant than listening for a back hardware button click.

Here was what i wanted to do:

I have two tabs, when the activity is being entered for the first time or the user has left the activity and is now entering it, a certain tab is displayed based on some criterion.

When the orientation changes, i need to stay on the same tab.


Solution 1:

Use the Activity's isFinishing() method.

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

Solution 2:

you can use isChangingConfigurations() Read from documentation

Check to see whether this activity is in the process of being destroyed in order to be recreated with a new configuration. This is often used in onStop() to determine whether the state needs to be cleaned up or will be passed on to the next instance of the activity via onRetainNonConfigurationInstance().

Returns If the activity is being torn down in order to be recreated with a new configuration, returns true; else returns false

Explain in simple way with example

isChangingConfigurations()

is method used to check if the activity will be destroyed to be re-created again (as result of change in orientation )

How to use it ?

if you use api >= 11 then no problem , but if you use api < 11 then we must handle this method manual I make boolean variable called IsconfigChange

private boolean IsconfigChange ;
...

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IsconfigChange = true ;

}


    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override
    public boolean isChangingConfigurations() {
        if(android.os.Build.VERSION.SDK_INT >= 11){
            Log.i("DEBUG", "Orientation changed api >= 11 ");
            return super.isChangingConfigurations();    
        }else {
            Log.i("DEBUG", "Orientation changed api < 11 ");
            return IsconfigChange; 
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override
    protected void onStop() {
        super.onStop();
        if(isChangingConfigurations()){
            Log.i("DEBUG", "isChangingConfirgurations OnStop Called");
        }  else{
            Log.i("DEBUG", "OnStop Called");
        }
    }

Summery

you can use isChangingConfigurations in onStop to check if app stop to be destroyed or due to change in orientation .

or you can use isFinishing check my answer here