How to distinguish between orientation change and leaving application android

AndroidLifecycle

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

Use the Activity's isFinishing() method.

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

Solution 2 - Android

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

Solution 3 - Android

for API lvl >= 11 Activity has a isChangingConfigurations() method

Solution 4 - Android

You could grab the value of the Activity.getChangingConfigurations() method in your onDestroy callback. This will return a result such as ORIENTATION_PORTRAIT, which you could check against your current orientation.

Note that the activity closing and orientation changes aren't the only conditions to consider here: What about returning to the Home screen, incoming phone calls and other apps stealing the focus, and all the other scenarios when your Activity is no longer at the front of the stack?

Most of the time you will not need to do this. If you are trying to fix some Activity state issue (often manifesting as a NullPointerException when you rotate the screen) by capturing the orientation event; review the Android Activity lifecycle and make sure you are not just trying to hack a fix for a design flaw. Post up your original problem on this site if you are unsure.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDustfingerView Question on Stackoverflow
Solution 1 - AndroiddmonView Answer on Stackoverflow
Solution 2 - AndroidMina FawzyView Answer on Stackoverflow
Solution 3 - AndroidMaurycyView Answer on Stackoverflow
Solution 4 - AndroidseanhodgesView Answer on Stackoverflow