片段导航到第二个时间表时会失去活动参考

发布于 2025-02-04 02:47:05 字数 3396 浏览 4 评论 0原文

我在单个活动应用中使用DrawerLayout和NavController用片段导航。但是,当我第二次转到任何片段时,当我调用getActivity()方法(返回null)时,我会失去对主要活动的引用。

这就是我在主要活动中启动导航的方式:

private fun initNavigation(){
        navController = Navigation.findNavController(this, R.id.fragment)


        NavigationUI.setupActionBarWithNavController(this,navController, drawerLayout)
        NavigationUI.setupWithNavController(navigationView, navController)

        navigationView.setNavigationItemSelectedListener(this)

        appBarConfiguration = AppBarConfiguration.Builder(
            navController.graph
        ).setDrawerLayout(drawerLayout)
            .build()



        setupActionBarWithNavController(navController,appBarConfiguration)

        navController.addOnDestinationChangedListener { controller, destination, arguments ->
            when(destination.id){
               R.id.searchFragment->  { supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_menu_icon)
               setTitleTop("Home")}
                R.id.historyFragment-> setTitleTop("History")
                R.id.myCollectionFragment -> setTitleTop("My Collection")
                R.id.settingsFragment-> setTitleTop("Settings")
                R.id.infoFragment-> setTitleTop("Details")

            }

        }

    }

这就是我从活动中导航的方式:

 override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            R.id.nav_home -> {
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.searchFragment)
            }
            R.id.nav_my_collection -> {
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.myCollectionFragment)

            }
            R.id.nav_wishlist -> {
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.wishlistFragment)


            }
            R.id.nav_history -> {
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.historyFragment)
            }
            R.id.nav_settings->{
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.settingsFragment)

            }
        }
        item.isChecked = true
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }

当我第二次导航到同一片段时,我的活动(或getActivity())为无效。当我尝试从片段中使用NavController时,我注意到了问题:

      override fun onRecordClicked(position: Int) {
        val bundle = Bundle().also {
            it.putSerializable(InfoFragment.RECORD_PARAM, viewModel.recordAdapter.records[position].record)
        }


        activity?.let {
            Navigation.findNavController(it, R.id.fragment)
                .navigate(R.id.infoFragment, bundle)
        }
        

    }

为了解决此问题,我在片段中实现了以下内容:

   lateinit var homeActivity : HomeActivity

   override fun onAttach(context: Context) {
        super.onAttach(context)
        if(context is HomeActivity)
            homeActivity = context
    }

然后使用此参考来导航:

        homeActivity.let {
            Navigation.findNavController(it,R.id.fragment)
                .navigate(R.id.infoFragment, bundle)
        }

即使我以此为目的,也不是那么聪明的方式,我也想了解为什么会发生这种情况?谢谢你的宝贵时间。

I use DrawerLayout and navController to navigate with fragments, in a single activity app. However, when I go to any of the fragments for the second time, I lose my reference to the main activity when I call the getActivity() method (returns null).

This is how i initiate navigation in main activity:

private fun initNavigation(){
        navController = Navigation.findNavController(this, R.id.fragment)


        NavigationUI.setupActionBarWithNavController(this,navController, drawerLayout)
        NavigationUI.setupWithNavController(navigationView, navController)

        navigationView.setNavigationItemSelectedListener(this)

        appBarConfiguration = AppBarConfiguration.Builder(
            navController.graph
        ).setDrawerLayout(drawerLayout)
            .build()



        setupActionBarWithNavController(navController,appBarConfiguration)

        navController.addOnDestinationChangedListener { controller, destination, arguments ->
            when(destination.id){
               R.id.searchFragment->  { supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_menu_icon)
               setTitleTop("Home")}
                R.id.historyFragment-> setTitleTop("History")
                R.id.myCollectionFragment -> setTitleTop("My Collection")
                R.id.settingsFragment-> setTitleTop("Settings")
                R.id.infoFragment-> setTitleTop("Details")

            }

        }

    }

And this is how I navigate from activity:

 override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            R.id.nav_home -> {
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.searchFragment)
            }
            R.id.nav_my_collection -> {
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.myCollectionFragment)

            }
            R.id.nav_wishlist -> {
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.wishlistFragment)


            }
            R.id.nav_history -> {
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.historyFragment)
            }
            R.id.nav_settings->{
                Navigation.findNavController(this, R.id.fragment)
                    .navigate(R.id.settingsFragment)

            }
        }
        item.isChecked = true
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }

When I navigate to the same fragment for the second time, my activity (or getActivity()) is null. I noticed the problem when I tried to use navController from my fragment:

      override fun onRecordClicked(position: Int) {
        val bundle = Bundle().also {
            it.putSerializable(InfoFragment.RECORD_PARAM, viewModel.recordAdapter.records[position].record)
        }


        activity?.let {
            Navigation.findNavController(it, R.id.fragment)
                .navigate(R.id.infoFragment, bundle)
        }
        

    }

To solve this I implemented the following in my fragment:

   lateinit var homeActivity : HomeActivity

   override fun onAttach(context: Context) {
        super.onAttach(context)
        if(context is HomeActivity)
            homeActivity = context
    }

And then used this reference to navigate:

        homeActivity.let {
            Navigation.findNavController(it,R.id.fragment)
                .navigate(R.id.infoFragment, bundle)
        }

Even though I solved it in this, not so smart way, I would like to understand why this happens? Thank you for your time.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文