To detect the rotation of the Activity without interfering with its course, you can do the following :
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
// Check for the rotation
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "LANDSCAPE", Toast.LENGTH_SHORT).show();
} else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "PORTRAIT", Toast.LENGTH_SHORT).show();
}
}
Additionally, you need you have to make sure that, on your manifest, your activity allows configChanges for the rotation
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
Important : As of Android 3.2 onwards (API 13+) , the "screen size" also changes when the device switches between portrait and landscape orientation.
If you want to prevent runtime restarts due to orientation change when developing for that specific API level (13+), make sure to declare : android:configChanges="orientation|screenSize"
-- // --
EDIT
Using the configChanges attribute, your Activity will not be recreated when configuration changes.
More can be found on Google's Documentation here
android:configChanges
Lists configuration changes that the activity will handle itself. When
a configuration change occurs at runtime, the activity is shut down
and restarted by default, but declaring a configuration with this
attribute will prevent the activity from being restarted. Instead, the
activity remains running and its onConfigurationChanged() method is
called.
What you can do is control the rotation with a flag, and if onConfigurationChanged() is called, you can call the Activity's onCreate() yourself. If not, onCreate() will be called only when the Activity is first instantiated