English 中文(简体)
未在喷气池堆肥中工作的 Back手
原标题:Backhandler not working in jetpack compose

Hey I is using BackHandler from this stackoverflow。 当我回头.子时,它不工作。 请允许我就此向我提供指导。

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))
        }
    }
}

如欲了解更多情况,请访问我的

<>>>>>

你们可以看到我的录像。 任何人都知道,我回过问,为什么我的幻灯片?

<><>UPDATE 2>

我在导航中添加了闪电屏幕。

@Composable
internal fun NavigationGraph() {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = ScreenRoute.Splash.route) {

        composable(route = ScreenRoute.Splash.route) {
            SplashScreen(navController = navController)
        }

        composable(ScreenRoute.Home.route) {
            SetupMainActivityView { nearestResult ->
                val nearestResultJson = Uri.encode(Json.encodeToString(nearestResult))
                navController.navigate(ScreenRoute.Result.route + "/$nearestResultJson") {
                    popUpTo(navController.graph.findStartDestination().id) {
                        saveState = true
                    }
                    launchSingleTop = true
                    restoreState = true
                }
            }
        }

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

<>9>

navController.navigate(ScreenRoute.Result.route + "/$nearestResultJson") {
                    popUpTo(ScreenRoute.Home.route) {
                        saveState = true
                    }
                    launchSingleTop = true
                    restoreState = true
                }

当我添加了闪闪电屏幕时,反之不行。

<>12>

@Composable
internal fun NavigationGraph() {
    val navController = rememberNavController()
    var home by remember {
        mutableStateOf<String?>(null)
    }
    NavHost(navController = navController, startDestination = home ?: ScreenRoute.Splash.route) {

        composable(route = ScreenRoute.Splash.route) {
            home = ScreenRoute.Home.route
            SplashScreen(navController = navController)
        }

        composable(ScreenRoute.Home.route) {
            SetupMainActivityView { nearestResult ->
                val nearestResultJson = Uri.encode(Json.encodeToString(nearestResult))
                navController.navigate(ScreenRoute.Result.route + "/$nearestResultJson") {
                    popUpTo(navController.graph.findStartDestination().id) {
                        saveState = true
                    }
                    launchSingleTop = true
                    restoreState = true
                }
            }
        }

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

My splash screen not working when initial application load. It direct open my home screen after going to result screen back button again not working.

问题回答

try navController.navigateUp() probably the stack has many entries, try using singleton navigation to that stack is not full of undesired entries like

navController.navigate("destination") {
                    popUpTo(navController.graph.findStartDestination().id) {
                        saveState = true
                    }
                    launchSingleTop = true
                    restoreState = true
                }

努力利用动态的起点,如

var home by remember {
        mutableStateOf<String?>(null)
    }
NavHost(
        navController = navController,
        startDestination = home?:"splash_destination",
        modifier = modifier
    ) {
composable("splash) {
            home = "home_destination"
          ....
        }
.....
}

然后采用以往的做法。

<>BackHandler不可行,因为你在<条码>上超越了<条码>。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签