在我的Android应用程序中,当我将方向从纵向改为水平再改回纵向时,屏幕只是一片空白。但是,当我点击屏幕时,UI重新出现。任何帮助都将不胜感激
它发生在我的应用程序中的所有屏幕上。下面是发生这种情况的屏幕示例:
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.navigation.NavHostController
@Composable
fun HomeScreen(navController: NavHostController) {
TopAppBar(title = {
Row(modifier = Modifier.fillMaxWidth()) {
Text(
text = "Home Screen",
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center
)
}
})
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Column() {
Text(text = FlashcardAppRoutes.Electricity.name, modifier = Modifier.clickable {
navController.navigate(
FlashcardAppRoutes.Electricity.name
)
})
}
}
}
1条答案
按热度按时间vsmadaxz1#
当方向发生变化时,Activity将被重新创建,这意味着onCreate()函数将被再次调用,保存在Activity类中的所有数据将丢失。要通过配置更改保存数据,我们必须将数据保存在ViewModel中,并将数据从该ViewModel收集到我们的Activity中。Refer here