34

Are there any uses for navigation with the viewpager? I can not find information about this and I do not understand how this can be done.

I have an simple two fragments which is need to put inside viewpager and if its possible via navigation.

Roman
  • 803
  • 2
  • 10
  • 17
  • 3
    Does this answer your question? [How to implement a ViewPager using new Navigation Architecture Component?](https://stackoverflow.com/questions/52630267/how-to-implement-a-viewpager-using-new-navigation-architecture-component) – Gk Mohammad Emon Apr 18 '20 at 08:13
  • @GkMohammadEmon It looks like a workaround, not a good approach. Navigation interferes and does not help. In my opinion quick answer on my question is NO. – Roman Apr 20 '20 at 15:47
  • 2
    This is not the duplicate of that question, that question is about ViewPager2 with BottomNavigationView. – Thracian Jun 16 '20 at 05:19
  • If you are looking for Navigation Component implementation only with ViewPager or ViewPager + TabLayout and back navigation with `OnBackPressedCallback` you can check out the answer i posted [here](https://stackoverflow.com/a/62530288/5457853) – Thracian Jun 26 '20 at 12:40

1 Answers1

39

As per this issue:

Navigation focuses on elements that affect the back stack and tabs do not affect the back stack - you should continue to manage tabs with a ViewPager and TabLayout

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • What do you think about this approach https://stackoverflow.com/a/54355659/6272369 ? – Marat Jan 25 '19 at 05:37
  • 2
    You should never use `BottomNavigationView` with a `ViewPager`, as per the [transitions sections of the material design guidelines](https://material.io/design/components/bottom-navigation.html#behavior), but having a separate navigation graph for each screen in the ViewPager is perfectly acceptable. – ianhanniballake Jan 25 '19 at 06:06
  • I have implemented BottomNavigationView and ViewPager with having a separate navigation graph for each screen in the ViewPager. https://stackoverflow.com/a/54569473/4723178 – Muhammad Maqsood Feb 07 '19 at 08:52
  • 3
    @MuhammadMaqsood - as per the [Transitions section of the Material Design guidelines for bottom nav](https://material.io/design/components/bottom-navigation.html#behavior), you should never be using a ViewPager and BottomNavigationView together. – ianhanniballake Feb 07 '19 at 13:22
  • There is a pretty confusing disconnect here between BottomNavigationView vs TabLayout back stacks: bottom bars work with the nav component using some custom nav controller behavior. However if you want a TabLayout instead, FragmentPagerAdapters use programmatic Fragments so while a similar behavior to bottom-nav views is required, I can't quite see a way to do it. – Daniel Wilson Apr 16 '19 at 15:00
  • 1
    I have my viewpager inside fragment, I want to start a new navigation graph for each fragment of viewpager. Can you give me a piece of code for, how to set NavHostFragment from the viewpager's fragment. – Swapnika May 15 '19 at 00:49
  • @Swapnika - as per [the documentation](https://developer.android.com/guide/navigation/navigation-programmatic#create_a_navhostfragment), you'd probably want to use `NavHostFragment.create()`. – ianhanniballake May 15 '19 at 00:53
  • 2
    @Swapnika you can take a look to my experiment here: https://github.com/muhrahmatullah/televisi – mangkool Sep 29 '19 at 15:24
  • "You should never use BottomNavigationView with a ViewPager" that's a strong statement. ViewPager2 has the option to disable the swiping gesture, multi-pane fragments inside one of the bottom nav pages is not uncommon. I found it's very convenient to use ViewPager(2) to implement my designers' flow. – X.Y. Dec 05 '19 at 20:53