How can I update remote data list when data updated?

Viewed 101

enter image description here

  1. Select in A Screen
  2. navigate to B Screen
  3. edit the product info in B Screen
  4. push Save Button, when update success, navigate up to A Screen
  5. A Screen show update list

Screen

sealed class Screen (
    val route: String
) {
    object List: Screen("product_list") //-->A Screen
    object EditProduct: Screen("edit_product") //-->B Screen
}

NavHost in MainActivity onCreate

        NavHost(navController = navController, startDestination = Screen.Splash.route) {
            
            composable(route = Screen.List.route) { 
                ListScreen()
            }
            composable(route = Screen.EditProduct.route) {
                EditProductScreen()
            }

A is a screen that brings up remote data and represents the list, and B contains detailed information about the product selected from A.

After entering the information about the product in B, I would like to update the list of A to show the revised information when I return to A by navigate up.

In this case, how should I deal with it?


LaunchedEffect(true) {
    viewModel.eventFlow.collectLatest { event ->
        when(event) {
            is UiEvent.SaveTask -> {
                //previousBackStackEntry?.savedStateHandle?.set<Boolean>("save_result", true)
                onSave()
            }
        }
    }
}

when I called onSave() --navcontroller::popBackStack, navigate to A Screen, but list isn't update

1 Answers

You cal pass a callback to your EditProductScreen and call navController.popBackStack() along with your list update code in this callback.

@Composable
fun EditProductScreen(onSave: () -> Unit) {
    Button(onClick = onSave) {
    }
}
composable(route = Screen.EditProduct.route) {
    EditProductScreen(
        onSave = {
            navController.popBackStack()
            // update your list
        }
    )
}
Related