@@ -91,51 +91,58 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
9191 if (screenOrientation == ScreenOrientation .LANDSCAPE ) Configuration .ORIENTATION_LANDSCAPE
9292 else Configuration .ORIENTATION_PORTRAIT
9393
94- if (configChangesHandled) {
95- Log .d(TAG , " The current activity handles configuration changes." )
96- context.registerComponentCallbacks(
97- object : ComponentCallbacks {
98- override fun onConfigurationChanged (newConfig : Configuration ) {
99- if (newConfig.orientation == requestedOrientation) {
100- if (Log .isLoggable(TAG , Log .DEBUG )) {
101- Log .d(TAG , " Application's orientation was set to the requested orientation." )
102- }
103- context.unregisterComponentCallbacks(this )
104- latch.countDown()
94+ val componentCallback =
95+ object : ComponentCallbacks {
96+ override fun onConfigurationChanged (newConfig : Configuration ) {
97+ if (newConfig.orientation == requestedOrientation) {
98+ if (Log .isLoggable(TAG , Log .DEBUG )) {
99+ Log .d(TAG , " Application's orientation was set to the requested orientation." )
105100 }
101+ latch.countDown()
106102 }
103+ }
107104
108- override fun onLowMemory () {}
105+ @Deprecated(" Deprecated in API 34" ) override fun onLowMemory () {}
106+ }
107+
108+ val activityLifecycleCallback =
109+ object : ActivityLifecycleCallback {
110+ override fun onActivityLifecycleChanged (activity : Activity , stage : Stage ) {
111+ if (
112+ activity.localClassName == currentActivityName &&
113+ stage == Stage .RESUMED &&
114+ activity.resources.configuration.orientation == requestedOrientation
115+ ) {
116+ if (Log .isLoggable(TAG , Log .DEBUG )) {
117+ Log .d(TAG , " Test activity was resumed in the requested orientation." )
118+ }
119+ latch.countDown()
120+ }
109121 }
110- )
122+ }
123+
124+ if (configChangesHandled) {
125+ Log .d(TAG , " The current activity handles configuration changes." )
126+ context.registerComponentCallbacks(componentCallback)
111127 } else {
112128 Log .d(
113129 TAG ,
114130 " The current activity does not handle configuration changes and will be recreated when " +
115- " its orientation changes."
131+ " its orientation changes." ,
116132 )
117- ActivityLifecycleMonitorRegistry .getInstance()
118- .addLifecycleCallback(
119- object : ActivityLifecycleCallback {
120- override fun onActivityLifecycleChanged (activity : Activity , stage : Stage ) {
121- if (
122- activity.localClassName == currentActivityName &&
123- stage == Stage .RESUMED &&
124- activity.resources.configuration.orientation == requestedOrientation
125- ) {
126- if (Log .isLoggable(TAG , Log .DEBUG )) {
127- Log .d(TAG , " Test activity was resumed in the requested orientation." )
128- }
129- ActivityLifecycleMonitorRegistry .getInstance().removeLifecycleCallback(this )
130- latch.countDown()
131- }
132- }
133- }
134- )
133+ ActivityLifecycleMonitorRegistry .getInstance().addLifecycleCallback(activityLifecycleCallback)
135134 }
135+
136136 deviceController.setScreenOrientation(screenOrientation.getOrientation())
137137 latch.await(5 , TimeUnit .SECONDS )
138138
139+ if (configChangesHandled) {
140+ context.unregisterComponentCallbacks(componentCallback)
141+ } else {
142+ ActivityLifecycleMonitorRegistry .getInstance()
143+ .removeLifecycleCallback(activityLifecycleCallback)
144+ }
145+
139146 // Restore accelerometer rotation setting if it was changed
140147 if (
141148 getDeviceApiLevel() >= 21 && startingAccelRotationSetting != getAccelerometerRotationSetting()
0 commit comments