8000 Backport request orientation cache · androidx/androidx@cd1add5 · GitHub
[go: up one dir, main page]

Skip to content

Commit cd1add5

Browse files
committed
Backport request orientation cache
API 35 / Android 15 added a field cache to `Activity.setRequestedOrientation()` because every call would lead to an IPC call, which can be expensive and hammer the system. This change backports the API 35 behavior. https://cs.android.com/android/_/android/platform/frameworks/base/+/14b59e53b82dbeb93ca1c8ac612c13d62c8672e9
1 parent 77715ed commit cd1add5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

core/core/src/main/java/androidx/core/app/ComponentActivity.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public open class ComponentActivity : Activity(), LifecycleOwner, KeyEventDispat
5050
*/
5151
@Suppress("LeakingThis") private val lifecycleRegistry = LifecycleRegistry(this)
5252

53+
private var lastRequestedOrientation: Int = ActivityInfo.SCREEN_ORIENTATION_UNSET
54+
5355
/**
5456
* Store an instance of [ExtraData] for later retrieval by class name via [getExtraData].
5557
*
@@ -110,6 +112,18 @@ public open class ComponentActivity : Activity(), LifecycleOwner, KeyEventDispat
110112
} else KeyEventDispatcher.dispatchKeyEvent(this, decor, this, event)
111113
}
112114

115+
override fun setRequestedOrientation(orientation: Int) {
116+
// Sets this Activity's orientation, only if the orientation value has changed or if a
117+
// value hasn't been previously set. This avoids unnecessary Binder calls, which
118+
// can be expensive. This caching was added to Activity.java in API level 35.
119+
if (Build.VERSION.SDK_INT >= 35) {
120+
super.setRequestedOrientation(orientation)
121+
} else if (orientation != lastRequestedOrientation) {
122+
lastRequestedOrientation = orientation
123+
super.setRequestedOrientation(orientation)
124+
}
125+
}
126+
113127
/**
114128
* Checks if the internal state should be dump, as some special args are handled by [Activity]
115129
* itself.

0 commit comments

Comments
 (0)
0