Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
a simplified code example of how Discord's Android app maintains panel states through device rotations while applying view updates before onLayout
class OverlappingPanelsLayout: FrameLayout {
private lateinit var startPanel: View
private lateinit var centerPanel: View
private lateinit var endPanel: View
private var pendingUpdate: (() -> Unit)? = null
private fun openStartPanel() {
// This can get called before onLayout() where centerPanel
// gets initialized. If that happens, save the pendingUpdate
// for after centerPanel gets initialized.
if (!::centerPanel.isInitialized) {
pendingUpdate = { openStartPanel(isFling) }
updateCenterPanelX(x = startPanelOpenedCenterPanelX)
override fun onLayout(
changed: Boolean,
left: Int,
top: Int,
right: Int,
bottom: Int
) {
super.onLayout(changed, left, top, right, bottom)
// OverlappingPanelsLayout expects exactly three child
// views where each child view is a panel. If there are
// not exactly three child views, OverlappingPanelsLayout will
// throw an exception.
if (!::centerPanel.isInitialized) {
startPanel = getChildAt(0)
centerPanel = getChildAt(1)
endPanel = getChildAt(2)
pendingUpdate = null
Copy link


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment