底部navigaiton视图中的导航创建/破坏片段导航2.4.2

发布于 2025-01-21 21:38:02 字数 2067 浏览 5 评论 0原文

我正在使用最新的导航版本2.4.2。

我设置了带有导航组件的底部导航栏,如下所示,与Google推荐的方式相同:

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val navView: BottomNavigationView = binding.navView

    val navController = findNavController(R.id.nav_host_fragment_activity_main)
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
        )
    )
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
}

我的菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/navigation_home"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/title_home" />

<item
    android:id="@+id/navigation_dashboard"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="@string/title_dashboard"
    xmlns:app="http://schemas.android.com/apk/res-auto" />

<item
    android:id="@+id/navigation_notifications"
    android:icon="@drawable/ic_notifications_black_24dp"
    android:title="@string/title_notifications" />

但是,当我从一个片段到另一个片段时,它的ondestroy()被调用,当我导航回到它时是重新创建。

除了开始时,底部navview中的所有片段都是这种情况。对于开始截止的onCreate()仅一次称为一次,当远离它时,仅调用ondestroyview()。我希望对所有其他片段的行为,以及我需要将代码放入onCreate()方法中,并希望它每次生命周期一次运行一次。

支持多个背面的支持使用导航2.4.0到达,所以我不知道怎么了。调用findnavcontroller.navigate(...)时,先前的片段保留在背包中,并未被破坏(),据我所知,底部navbar调用了相同的方法,所以我无法弄清楚为什么每个片段都在在每个导航上创建/破坏。

我从:开始片段 - &gt;仪表板碎片 - &gt;导航片段 - &gt;开始片段

“

重新创建了启动片段以外的所有片段。

任何帮助都将受到赞赏。

I am using the latest Navigation version 2.4.2.

I set up the bottom nav bar with the Navigation component as follow, the same way recommended by google:

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val navView: BottomNavigationView = binding.navView

    val navController = findNavController(R.id.nav_host_fragment_activity_main)
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
        )
    )
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
}

My menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/navigation_home"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/title_home" />

<item
    android:id="@+id/navigation_dashboard"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="@string/title_dashboard"
    xmlns:app="http://schemas.android.com/apk/res-auto" />

<item
    android:id="@+id/navigation_notifications"
    android:icon="@drawable/ic_notifications_black_24dp"
    android:title="@string/title_notifications" />

However, when I navigate from one fragment to the other, its onDestroy() is called and when I navigate back to it is recreated.

This is the case for all the fragment in the BottomNavView except the startDestination. The onCreate() for the startDestination is called only once and when navigating away from it, only the onDestroyView() is called. I want this behavior for all the other fragments as well as I need to put code in the onCreate() method and want it to run once once per lifecycle of the app.

Support for multiple backstack arrive with Navigation 2.4.0 so I don't know what's wrong. when calling findNavController.navigate(...), the previous fragment is kept in the backstack and is not destroyed(), and as far as I know the BottomNavBar calls the same method so I can't figure out why each fragment is being created/destroyed upon each navigation.

Me navigating from : Start Fragment -> Dashboard Fragment -> Navigation Fragment -> Start Fragment

enter image description here

All the fragment except the Start Fragment is recreated.

Any help is appreciated.

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

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

发布评论

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

评论(1

七月上 2025-01-28 21:38:02

对于任何寻找这个问题答案的人。 Google回答说这是预期的行为。

https://issuetracker.google.com/issues/issues/issues/190893266

For anyone looking for an answer to this question. Google replied that it is expected behaviour.

https://issuetracker.google.com/issues/190893266

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