fix(state): Preserve UI state on screen rotation
This commit addresses the critical issue of UI state loss on screen rotation. - The `Screen` sealed class is now made `Parcelable` using the `kotlin-parcelize` plugin, ensuring that navigation state (`currentScreen`, `selectedListId`) is preserved. - Dialog visibility state (`showListDialog`) is also preserved using `rememberSaveable`. - This prevents the app from navigating back to the list overview and losing input field/dialog state when the device orientation changes.
This commit is contained in:
@@ -3,6 +3,7 @@ plugins {
|
||||
alias(libs.plugins.kotlin.android)
|
||||
alias(libs.plugins.google.devtools.ksp)
|
||||
}
|
||||
apply(plugin = "kotlin-parcelize")
|
||||
|
||||
android {
|
||||
namespace = "de.lxtools.noteshop"
|
||||
|
||||
@@ -56,6 +56,7 @@ import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
@@ -71,12 +72,15 @@ import de.lxtools.noteshop.ui.shopping.ShoppingListsScreen
|
||||
import de.lxtools.noteshop.ui.shopping.ShoppingListsViewModel
|
||||
import de.lxtools.noteshop.ui.theme.NoteshopTheme
|
||||
import kotlinx.coroutines.launch
|
||||
import android.os.Parcelable
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
// Sealed class to represent the screens in the app
|
||||
sealed class Screen(val route: String, val titleRes: Int) {
|
||||
object ShoppingLists : Screen("shopping_lists", R.string.menu_shopping_lists)
|
||||
object Notes : Screen("notes", R.string.menu_notes)
|
||||
object ShoppingListDetail : Screen("shopping_list_detail", R.string.menu_shopping_list_detail) // New screen
|
||||
@Parcelize
|
||||
sealed class Screen(val route: String, val titleRes: Int) : Parcelable {
|
||||
data object ShoppingLists : Screen("shopping_lists", R.string.menu_shopping_lists)
|
||||
data object Notes : Screen("notes", R.string.menu_notes)
|
||||
data object ShoppingListDetail : Screen("shopping_list_detail", R.string.menu_shopping_list_detail) // New screen
|
||||
}
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
@@ -99,10 +103,10 @@ fun AppShell(
|
||||
) {
|
||||
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
|
||||
val scope = rememberCoroutineScope()
|
||||
var currentScreen: Screen by remember { mutableStateOf(Screen.ShoppingLists) }
|
||||
var selectedListId: Int? by remember { mutableStateOf(null) }
|
||||
var currentScreen: Screen by rememberSaveable { mutableStateOf(Screen.ShoppingLists) }
|
||||
var selectedListId: Int? by rememberSaveable { mutableStateOf(null) }
|
||||
|
||||
var showListDialog by remember { mutableStateOf(false) }
|
||||
var showListDialog by rememberSaveable { mutableStateOf(false) }
|
||||
val listDetails by shoppingListsViewModel.listDetails.collectAsState()
|
||||
|
||||
val navigationItems = listOf(
|
||||
|
||||
Reference in New Issue
Block a user