I have an issue very similar to this: Android - ActionBar not resizing with onConfigurationChanged ( AppCompat )
I need to let android handle orientation changes, because i want to get the activity recreated.
But also, i need to detect when a orientation change has been produced.
Are these two combined needs able to be achieved at once?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With