0

I've just started using Navigation Component library. I've implemented a bottom navigation view and wired it up with NavController. The problem is, every time I click the navigation icon, the associated fragment gets recreated. I want it to reuse the fragment in back stack. Can anyone give me a solution.

Note: I have tried to use OnNavigationItemSelecetedListener. It doesn't seem to override the bottom nav click events.

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Mustaqode
  • 453
  • 1
  • 4
  • 14
  • 3
    Does this answer your question? [Android navigation component: how save fragment state](https://stackoverflow.com/questions/56195791/android-navigation-component-how-save-fragment-state) – EraftYps Jun 26 '20 at 18:20
  • Yes. So there is no direct implementation in Navigation Component Library to achieve this. Anyway, thanks. @EraftYps – Mustaqode Jun 27 '20 at 07:12

1 Answers1

1

Bad news, Navigation Component was meant to be used with ViewModels, since their stances remain alive after transitions. (I'm assuming you are not using ViewModels)

You can save the fragment's instance in a variable and start that instance in OnCreate when it's not null. This is more of a hack than a solution since is not advised because its propense to generate memory leaks.

I'm dealing with the same problem myself, I will update my answer once I get to my computer.

Miguel Manzo
  • 171
  • 9