You want to read the section called Handling the Configuration Change Yourself in the Handling Runtime Changes document. In short, you tell it you will handle them manually by adding android:configChanges="orientation|keyboardHidden"
to your Activity decleration in your AndroidManifest. Here is an example:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
And then in your Activity you override onConfigurationChanged
like this:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}