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
onDestroy / onCreate
will not be called - and instead your activity'sonConfigurationChange
will be called. You can do whatever is necessary in there. – Papillaandroid:configChanges
, "Using this attribute should be avoided and used only as a last-resort." – MandatoryBundle
supplied toonSaveInstanceState()
, and restore the selected tab inonRestoreInstanceState()
. – Mandatory