简体   繁体   English

导航回时避免 API 调用可组合项

[英]Avoid API call in composable when navigating back to it

What I'm trying to do is avoid to make the api call that is called in a composable when press back from another composable.我想要做的是避免在从另一个可组合项按回时进行在可组合项中调用的 api 调用。

I have composable DetailScreen that makes the api call and get some data and then navigate to composable EpisodeScreen, here is the thing, when I press back it's navigate back to composable DetailScreen but it makes de api call again and that is what I'm tryin to avoid.我有可组合的 DetailScreen,它可以调用 api 并获取一些数据,然后导航到可组合的 EpisodeScreen,事情就是这样,当我按回它时,它会导航回可组合的 DetailScreen,但它会再次调用 api,这就是我正在尝试的避免。

Here is my Composable DetailScreen:这是我的可组合 DetailScreen:

@Composable
fun DetailScreen(
    viewModel: DetailViewModel = hiltViewModel(),
    id: Int,
    name: String,
    picture: String,
    onClickBack: () -> Unit,
    onClickEpisode: (Int) -> Unit
) {

   
    LaunchedEffect(key1 = Unit) {
        //this is the line I'm trying to avoid when navigate back from composable EpisodeScreen
        viewModel.getCharacterDetail(id)
    }

...

Here is the navigation to composable EpisodeScreen:这是可组合 EpisodeScreen 的导航:

private fun NavGraphBuilder.addDetailGraph(navController: NavHostController) {
    composable(
        route = Screen.Detail.route,
        arguments = listOf(
            navArgument("id") { type = NavType.IntType },
            navArgument("name") { type = NavType.StringType },
            navArgument("image") { type = NavType.StringType }
        )
    ) { backStackEntry ->
        val id = backStackEntry.arguments?.getInt("id") ?: 0
        val name = backStackEntry.arguments?.getString("name") ?: ""
        val image = backStackEntry.arguments?.getString("image") ?: ""

        DetailScreen(
            id = id,
            picture = image,
            name = name,
            onClickBack = {
                navController.popBackStack()
            },
            onClickEpisode = { episodeId ->
                navController.navigate(
                    Screen.Episode.createRoute(id = episodeId)
                ) 
            }
        )
    }
}

I tried to keep a state with remember and remembersaveable and put it as parameter in LaunchEffect() but nothing and I can´t find info or I can see it我试图用 remember 和 remembersaveable 保留一个 state 并将其作为参数放入 LaunchEffect() 但什么也没有,我找不到信息或者我可以看到它

Can anyone help me with this??谁能帮我这个?? thanks in advance:)提前致谢:)

Your DetailViewModel instance will still be alive when you navigate to the Episode screen, so you can put some logic there.当您导航到 Episode 屏幕时,您的 DetailViewModel 实例仍然处于活动状态,因此您可以在那里放置一些逻辑。 You can do one of the following:您可以执行以下操作之一:

  • Create a boolean in your ViewModel, initially set to false .在您的 ViewModel 中创建一个 boolean,最初设置为false Inside the setCharacter function, check the value of this variable.在 setCharacter setCharacter里面,检查这个变量的值。 If it's true just return otherwise change it to true and execute rest of the code.如果为true ,则返回,否则将其更改为true并执行代码的 rest。 Or if you already have a variable in ViewModel which is initialized inside setCharacter , you can use that instead of that boolean.或者,如果您在 ViewModel 中已经有一个在 setCharacter 中初始化的变量,您可以使用它来代替setCharacter

  • Another option is to call in the setCharacter function inside the init block of the view model.另一种选择是在视图setCharacterinit块内调用 setCharacter function。 And you can get the id from the SavedStateHandle that you can inject in the view model.您可以从SavedStateHandle获取id ,您可以将其注入视图 model。

in viewmodel add following variable在视图模型中添加以下变量

var isLoaded = true变量 isLoaded = true

and in launchedEffect并在 launchedEffect

    @Composable
    fun DetailScreen(
        viewModel: DetailViewModel = hiltViewModel(),
        id: Int,
        name: String,
        picture: String,
        onClickBack: () -> Unit,
        onClickEpisode: (Int) -> Unit
    ) {
    
      
        LaunchedEffect(key1 = Unit) {
            // add this check
            if (viewModel.isLoaded) {
            viewModel.getCharacterDetail(id) }
}

when navigating to detail screen and then navigate back -> recomposition take place and code inside launchedEffect will run again.当导航到详细信息屏幕然后导航回来时 -> 发生重组并且 launchedEffect 中的代码将再次运行。 To remove this add above checking.要删除此添加上面的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当按下后退按钮时,避免活动的onCreate()函数调用 - avoid onCreate() function call of activity when back button is pressed 在活动之间来回导航时,如何释放图像并避免outOfMemory exc-n [android] - How do I release images and avoid outOfMemory exc-n when navigating back and forth between activities[android] 当我按回来时导航片段的问题 - Problem with navigating fragments when i press on back 返回时刷新viewmodel中的数据,android - refresh data in viewmodel when navigating back, android 导航返回时的 RecyclerView(导航组件) - RecyclerView when navigating back (Navigation Component) 导航回MvxTabsFragmentActivity时,应用程序崩溃 - App crashes when navigating back to MvxTabsFragmentActivity 导航回来时,Android工具栏变得半透明 - Android Toolbar becomes translucent when navigating back 通过 navGraphViewModel 导航返回时保留 ViewModel 实例 - Keep ViewModel instance when navigating back with by navGraphViewModel 使用 Jetpack Compose 的深层链接导航到可组合项 - Navigating to a composable using a deeplink with Jetpack Compose 使用导航组件来回导航时,ViewPager的内容消失 - Content of ViewPager disappears when navigating back and forth using Navigation Components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM