0

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.

PhongBM
  • 799
  • 10
  • 23
  • I have not found a solution yet, pls help me. – PhongBM Sep 01 '20 at 17:25
  • Does this answer your question? [Android - PageView in new BottomNavigationBar - prevent reload fragments](https://stackoverflow.com/questions/44375904/android-pageview-in-new-bottomnavigationbar-prevent-reload-fragments) – Lalit Fauzdar Sep 03 '20 at 15:57
  • The solution is to use `FragmentManager`, add all the `Fragments` to it in `onCreate()` and show/hide the `Fragments` as required. Check the above answer I've mentioned for that. – Lalit Fauzdar Sep 03 '20 at 15:59
  • Thank for support, but I want to use Navigation Component with BottomNaviagtionView. – PhongBM Sep 03 '20 at 16:40
  • That's why I shared that answer. It doesn't use `ViewPager`. – Lalit Fauzdar Sep 03 '20 at 17:29

1 Answers1

0

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
            }
        }
    }
Mieszko Koźma
  • 460
  • 4
  • 10