Search code examples
androidorientationscreen-orientationandroid-orientationonconfigurationchanged

onConfigurationChanged is not getting triggered


I have to detect when the configuration has changed. I have put the configChanges conditions in the manifest

    <activity
        android:name=".WorkoutActivity"
        android:configChanges="orientation|keyboardHidden|screenSize">
    </activity>

There are no setRequestedOrientation in the activity or in any fragment. Here is the code for configuration change:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Toast.makeText(this, "activity rotated", Toast.LENGTH_SHORT).show();
}

Solution

  • The reason for the rotation not being detected was because I had set the device rotation to portrait (canceled autorotate) on my mobile phone. This prevented my app from rotating. Found the answer here: onConfigurationChanged not getting called

    The way to override this preference set by the user is to add the following code to the manifest as answered here: onConfigurationChanged is not getting called any time in android

    android:screenOrientation="sensor"
    

    Now the app rotates based on the sensor input and onConfigurationChanged is triggered. I hope this answer helps others get rid of this potential bug.