简体   繁体   中英

Navigation Component - BottomNavigationView - Don't keep startDestination fragment when back

I'm setup UI with Navigation Component and BottomNavigationView.

3 tabs: Home, News, Account.

When I navigate from Home to News (currently, Back stack has HomeFragment) and press Back button, fragment News will be destroyed, HomeFragment in Back stack will be destroyed too and create new HomeFragment. I expect HomeFragment in Back stack show again with not destroy and create new.

I use this code:

bottomNavigation.setupWithNavController(navHostFragment.findNavController())

How to fix this issue?

Thanks.


Update question:

When I clicked menu item, Fragment will be create new instance. I don't want that, I want to navigate to new Fragment if it hasn't been clicked yet, and the Fragment will be show again (without create new) if I click menu item again.

Regarding your updated question. If you wish not to recreate fragment on reselection, simply set OnNavigationItemReselectedListener on the BottomNavigationView

    bottomNavigation.setOnNavigationItemReselectedListener { item ->
        when (item.itemId) {
            R.id.frag1-> {
                // do something on reselection or leave empty
            }
            R.id.frag2-> {
                // do something on reselection or leave empty
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM