0
  1. 从一项活动更改为另一项活动后,我正在尝试保存我的编辑文本值
  2. 例如:就像在注册寻呼机中一样(首先您必须在页面中填写所有数据,最后提交。
  3. 我在这里遇到问题是如果我想编辑我的第一页数据所以我从第二页移动到第一页但我的第一页没有显示任何数据
  4. 我尝试使用保存状态和恢复导航状态,但无济于事
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent { ScreenNavigation() }
    }
}

@Composable
fun Page1(navController: NavHostController) {
    var test = remember { mutableStateOf("") }

    Column() {
        TextField(value = test.value, onValueChange = { test.value = it })
        Button(
            onClick = {
                navController.navigate(ScreenRoute.Page2.name) {
                    popUpTo(ScreenRoute.Page2.name) { saveState = true }
                    launchSingleTop = true

                    restoreState = true
                }
            }
        ) {}

        Text(text = "Page1")
        TextField(
            value = test.value,
            onValueChange = { test.value = it },
        )
    }
}

@Composable
fun Page2(navController: NavHostController) {
    var test = remember { mutableStateOf("") }

    Column() {
        TextField(value = test.value, onValueChange = { test.value = it })
        Button(
            onClick = {
                navController.navigate(ScreenRoute.Page1.name) {
                    popUpTo(ScreenRoute.Page1.name) { saveState = true }
                    launchSingleTop = true

                    restoreState = true
                }
            }
        ) {}
        Text(text = "Page2")
    }
}

// code for navigation

@Composable
fun ScreenNavigation() {
    val navController = rememberNavController()

    NavHost(navController = navController, startDestination = ScreenRoute.Page1.name) {
        composable(ScreenRoute.Page1.name) { Page1(navController) }

        composable(ScreenRoute.Page2.name) { Page2(navController) }
    }
}

// code for screen route
enum class ScreenRoute {
    Page1,
    Page2,
}
4

2 回答 2

1

将两个变量都放在函数之外和上面的类中,如下所示:

var test1 = mutableStateOf("")
var test2 = mutableStateOf("") 

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent { ScreenNavigation() }
    }
}

@Composable
fun Page1(navController: NavHostController) {
    

    Column() {
        TextField(value = test1.value, onValueChange = { test1.value = it })
        Button(
            onClick = {
                navController.navigate(ScreenRoute.Page2.name) {
                    popUpTo(ScreenRoute.Page2.name) { saveState = true }
                    launchSingleTop = true

                    restoreState = true
                }
            }
        ) {}

        Text(text = "Page1")
        TextField(
            value = test1.value,
            onValueChange = { test1.value = it },
        )
    }
}

@Composable
fun Page2(navController: NavHostController) {


    Column() {
        TextField(value = test2.value, onValueChange = { test2.value = it })
        Button(
            onClick = {
                navController.navigate(ScreenRoute.Page1.name) {
                    popUpTo(ScreenRoute.Page1.name) { saveState = true }
                    launchSingleTop = true

                    restoreState = true
                }
            }
        ) {}
        Text(text = "Page2")
    }
}

// code for navigation

@Composable
fun ScreenNavigation() {
    val navController = rememberNavController()

    NavHost(navController = navController, startDestination =       ScreenRoute.Page1.name) {
        composable(ScreenRoute.Page1.name) { Page1(navController) }

        composable(ScreenRoute.Page2.name) { Page2(navController) }
    }
}

// code for screen route
enum class ScreenRoute {
    Page1,
    Page2,
}
于 2021-12-30T13:42:29.040 回答
0
  1. 两个变量都在函数之外
  2. 使用 rememberSaveable
  3. remembersaveable 保存值的状态
       val numberOfFamilyMember = rememberSaveable {
       mutableStateOf("")
   }

于 2022-01-04T05:37:51.917 回答