Fix pager state after configuration change
When we declare android:configChanges="orientation" in the manifest, the
pager state is in a weird state after configuration change.
Reset pager state to work around this issue after configuration change.
TODO: Remove this work around after the Compose Foundation 1.5.0-alpha04
updated in the platform.
Bug: 281928661
Test: Manually with Gallery
Test: Manually with Settings
Change-Id: I8fa1a9603f67147cdacb0e0ab1a3f8a67286a995
This commit is contained in:
@@ -20,12 +20,21 @@ import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.pager.HorizontalPager
|
||||
import androidx.compose.foundation.pager.rememberPagerState
|
||||
import androidx.compose.foundation.pager.PagerState
|
||||
import androidx.compose.material3.TabRow
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.SideEffect
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import com.android.settingslib.spa.framework.theme.SettingsDimension
|
||||
import kotlin.math.absoluteValue
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -41,7 +50,7 @@ fun SettingsPager(titles: List<String>, content: @Composable (page: Int) -> Unit
|
||||
|
||||
Column {
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val pagerState = rememberPagerState()
|
||||
val pagerState by rememberPageStateFixed()
|
||||
|
||||
TabRow(
|
||||
selectedTabIndex = pagerState.currentPage,
|
||||
@@ -69,3 +78,28 @@ fun SettingsPager(titles: List<String>, content: @Composable (page: Int) -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the state of [PagerState].
|
||||
*
|
||||
* This is a work around.
|
||||
*
|
||||
* TODO: Remove this and replace with rememberPageState() after the Compose Foundation 1.5.0-alpha04
|
||||
* updated in the platform.
|
||||
*/
|
||||
@Composable
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
private fun rememberPageStateFixed(): State<PagerState> {
|
||||
var currentPage by rememberSaveable { mutableStateOf(0) }
|
||||
val pagerStateHolder = remember { mutableStateOf(PagerState(currentPage)) }
|
||||
LaunchedEffect(LocalConfiguration.current.orientation) {
|
||||
// Reset pager state to fix an issue after configuration change.
|
||||
// When we declare android:configChanges="orientation" in the manifest, the pager state is
|
||||
// in a weird state after configuration change.
|
||||
pagerStateHolder.value = PagerState(currentPage)
|
||||
}
|
||||
SideEffect {
|
||||
currentPage = pagerStateHolder.value.currentPage
|
||||
}
|
||||
return pagerStateHolder
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user