简体   繁体   中英

Backhandler not working in jetpack compose

Hey I am using BackHandler from this stackoverflow . When I am pressing back button it's not working. Can someone guide me on this.

ResultScreen.kt

@Composable
fun ResultScreen(navController: NavHostController, nearestResultList: List<NearestResult>?) {
    SportsResultTheme {
        MainScaffold {
            BackHandler {
                navController.popBackStack()
            }
            LazyColumn {
                if (nearestResultList != null) {
                    items(nearestResultList) { nearestResult ->
                        Text(
                            text = nearestResult.event
                        )
                    }
                }
            }
        }
    }
}

NavigationGraph.kt

@Composable
internal fun NavigationGraph() {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = ScreenRoute.Home.route) {
        composable(ScreenRoute.Home.route) {
            SetupMainActivityView { nearestResult ->
                val nearestResultJson = Uri.encode(Json.encodeToString(nearestResult))
                navController.navigate(ScreenRoute.Result.route + "/$nearestResultJson")
            }
        }

        composable(
            ScreenRoute.Result.route + "/{$NEAREST_RESULT_JSON}",
            arguments = listOf(
                navArgument(NEAREST_RESULT_JSON) { type = NearestResultParamType() }
            )
        ) { backStackEntry ->
            ResultScreen(navController, backStackEntry.arguments?.getParcelableArrayList(NEAREST_RESULT_JSON))
        }
    }
}

If you want to see more please visit my repository .

UPDATE

You can see my video link . Anyone know when I back press why my scren flicks ?

尝试navController.navigateUp()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM