@@ -91,51 +91,58 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
91
91
if (screenOrientation == ScreenOrientation .LANDSCAPE ) Configuration .ORIENTATION_LANDSCAPE
92
92
else Configuration .ORIENTATION_PORTRAIT
93
93
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." )
105
100
}
101
+ latch.countDown()
106
102
}
103
+ }
107
104
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
+ }
109
121
}
110
- )
122
+ }
123
+
124
+ if (configChangesHandled) {
125
+ Log .d(TAG , " The current activity handles configuration changes." )
126
+ context.registerComponentCallbacks(componentCallback)
111
127
} else {
112
128
Log .d(
113
129
TAG ,
114
130
" The current activity does not handle configuration changes and will be recreated when " +
115
- " its orientation changes."
131
+ " its orientation changes." ,
116
132
)
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)
135
134
}
135
+
136
136
deviceController.setScreenOrientation(screenOrientation.getOrientation())
137
137
latch.await(5 , TimeUnit .SECONDS )
138
138
139
+ if (configChangesHandled) {
140
+ context.unregisterComponentCallbacks(componentCallback)
141
+ } else {
142
+ ActivityLifecycleMonitorRegistry .getInstance()
143
+ .removeLifecycleCallback(activityLifecycleCallback)
144
+ }
145
+
139
146
// Restore accelerometer rotation setting if it was changed
140
147
if (
141
148
getDeviceApiLevel() >= 21 && startingAccelRotationSetting != getAccelerometerRotationSetting()
0 commit comments