停止使用Navgraph的底部逐步片段重新创建

发布于 2025-02-12 13:28:47 字数 851 浏览 2 评论 0原文

我有多个片段,例如仪表板通知 profile 在底部导航中。我正在使用Navgraph- NavController来控制片段。

我想保存以前片段的状态。

我不想在片段之间切换时再次致电我的API。每当我在片段之间切换时,都会调用当前fragmnent的先前片段和onCreateview的ondestroyview。这就是为什么OnCreateview或OnViewCreated中的所有操作都会再次致电的原因。

我怎么能摆脱它。是否使用 navgraph 进行任何实现,以阻止片段进行再造,

或者有办法不再次称呼这些API。我的意思是次要脱位。

例如: 用户在地图上 fragment,他在Google地图上搜索某些位置,然后移至下一个片段仪表板

我尝试使用

lifecycleScope.launch {
   lifecycleScope.launchWhenCreated {
        Log.v("LifeCycleState","launchWhenCreated")
   }
}
viewLifecycleOwner.lifecycleScope.launch {
    lifecycleScope.launchWhenCreated {
         Log.v("LifeCycleState","launchWhenCreated in viewLifeCycleOwner")
    }
}

I have multiple fragments like Dashboard,Notifications and Profile in Bottom Navigation. I am using NavGraph- NavController to control the fragments.

I want to save the state of previous fragment.

I don't want to call my API's again on Switching between fragments. Whenever I switch between fragment it calls onDestroyView of previous fragment and onCreateView of current Fragmnent . That's why all the operation in onCreateView or onViewCreated will call again.

How could I get rid of it. Is there any implementation using NavGraph that stop fragment from reCeating

Or is there a way to don't call those API's again . I mean to retatin the UIState.

For example:
The user is on Map fragment and he search some location on Google Map and moves to the next fragment Dashboard

I have tried using

lifecycleScope.launch {
   lifecycleScope.launchWhenCreated {
        Log.v("LifeCycleState","launchWhenCreated")
   }
}
viewLifecycleOwner.lifecycleScope.launch {
    lifecycleScope.launchWhenCreated {
         Log.v("LifeCycleState","launchWhenCreated in viewLifeCycleOwner")
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱情眠于流年 2025-02-19 13:28:47

您要遵循多导航图概念。

为基于底部的导航图标创建单独的图表。 ex Home,历史和帐户,因此您可以创建3个图形并将其包含在一个图中。

像这样,

<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@+id/home">

<include app:graph="@navigation/home"/>
<include app:graph="@navigation/history"/>
<include app:graph="@navigation/account"/>

请按照以下内容 sample

You've to follow multi navigations graph concept.

Create separate graphs for the bottom nav icon based. Ex Home, History and Account So you've to create 3 graphs and include in one graph.

Like this

<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@+id/home">

<include app:graph="@navigation/home"/>
<include app:graph="@navigation/history"/>
<include app:graph="@navigation/account"/>

Follow this sample

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文