如何在片段之间切换?

发布于 2025-01-23 13:21:02 字数 815 浏览 0 评论 0 原文

我有3个片段,我正在用nav_graph控制它们。第一个是ListingEvents,第二个是ListingItemsofevents,最后一个是更新的项目。 当我更新项目并单击“ Update”按钮时,它会输入VolistingItemsofevents片段,这很好。但是,在ListInListingItemsofevents上,当我单击“返回”按钮时(我在第二张图片上绘制蓝色),我想打开Listingingevents Fragment。使用我的代码,它返回更新片段。当我单击“后面”按钮时,如何打开ListingEvents片段(我在第二张图片上画了蓝色)?

”在此处输入图像描述”

navGraph

I have 3 fragments and I am controlling them with a nav_graph. The first one is listingEvents, the second one is listingItemsOfEvents and the last one is updateListing items.
When I update items and click "Update" button, It goes tolistingItemsOfEvents fragment, it is nice. However on listinlistingItemsOfEvents when I click the back button(I drew blue color on second picture) I want to open listingEvents fragment. With my codes it returns update fragments. How I can open listingEvents fragment when I click the back button(I drew blue color on second picture)?

Update Fragment

enter image description here

navGraph

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

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

发布评论

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

评论(1

知你几分 2025-01-30 13:21:03

要在用户从操作栏中选择时处理导航,请在您的主机活动中使用return navcontroller.navigateup()覆盖onsupportNavigateUp,

class MainActivity : AppCompatActivity(R.layout.activity_main) {

    private lateinit var navController: NavController

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

        // Retrieve NavController from the NavHostFragment
        val navHostFragment = supportFragmentManager
                .findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController

        // Set up the action bar for use with the NavController
        setupActionBarWithNavController(navController)
    }

    /**
     * Handle navigation when the user chooses Up from the action bar.
     */
    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
}

To handle navigation when the user chooses Up from the action bar, override onSupportNavigateUp with returning navController.navigateUp() in your host activity like this,

class MainActivity : AppCompatActivity(R.layout.activity_main) {

    private lateinit var navController: NavController

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

        // Retrieve NavController from the NavHostFragment
        val navHostFragment = supportFragmentManager
                .findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController

        // Set up the action bar for use with the NavController
        setupActionBarWithNavController(navController)
    }

    /**
     * Handle navigation when the user chooses Up from the action bar.
     */
    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文