I am developing a single activity application. It means I have a host activity and several different fragments. In the manifest, there is an attribute for activity android:screenOrientation="portrait" to suppress screen rotation. But in one of my fragments, I need to handle the device rotation to enable landscape orientation. Is there any way to handle the configuration change with attribute screenOrientation="portrait" for a specific fragment?
Asked
Active
Viewed 284 times
0
-
1for the fragment, there is no config like Activity in XML as you asked. But you can set orientation using code for single fragment see https://stackoverflow.com/questions/13251930/allow-rotation-landscape-in-one-fragment – Pankaj Kumar May 01 '20 at 15:55
1 Answers
0
I found an elegant solution for such case. In the specific fragment just set required orientation.
override fun onResume() {
super.onResume()
requireActivity().requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
override fun onPause() {
super.onPause()
requireActivity().requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}

DanMan
- 692
- 9
- 22