Error while calling Navigation Components while using ViewPager 2 - android

I have a hard time with trying to learn Navigation Components.
In my app, I have 6 fragments.
Four of them are swipeable by using ViewPager 2
Fifth fragment is not implemented in ViewPager, so user can't go there by swipping
Sixth fragment is just a host for ViewPager, since the other fragments are used by Navigation Components
I can swipe fragments for now, but whenever I try to use Navigation Components, I getting error about action/destionation. It's simply about calling for example ActionFrag1ToFrag5 from my Host Fragment
It seems like I can browse through fragments, but I can't cast any Navigation Component functions, because the fragment stays the same, it doesn't change label after swipe to another one.
Is there a way to get proper NavController from specific fragment, or it's just my poor implementation?

If Frag1, Frag2, Frag3, and Frag4 are those in the ViewPager and you want to go to Frag5.
The direction needs to be from the Frag6 that is hosting the ViewPager to the Frag5.

Related

show content of recyclcer view in fragment and bottomnavigation using navigation jetpack

I implemented the bottom navigation with Jetpack
in one tab i have recyclerview
I want to enter the details of an item when it is clicked and gone bottom navigation
What is the best way to do this?
using activity or fragment?
Fragments live within the same activity lifecycle, which means that switching from one tab to another won't destroy the activity data.
Fragments are therefore the best choice.
Here is a link to a step by step procedure on the implementation.
https://proandroiddev.com/step-by-step-to-bottom-navigation-with-jetpacks-navigation-component-and-multiple-nav-graphs-271c05af1dd3

How do I retain the state of my ViewPager in my TabLayout when I'm replacing a Fragment on top of it?

I have a single activity that uses a bunch of different Fragments. I have a TabContainer Fragment that holds a TabLayout which uses a ViewPager to handle tab navigation. Each Tab is its own Fragment.
In one of my tabs, I want to tap and place a fragment on top of my Tabbed fragment. This is meant to be a "details" sort of screen, so I don't want the tabs to be visible. I'm using this and it works as intended:
fragmentTransaction.replace(android.R.id.content, fragmentToDisplay).addToBackStack(null).commit();
Now, when I navigate back, the content in my tab is empty. The content in the tab directly next to that tab is also empty. When I navigate two tabs away, the content is recreated and the normal functionality returns. Why is content not being recreated on the tabs initially when I remove my "detail" fragment?
It turns out that I was simply not passing the correct FragmentManager to the FragmentStatePagerAdapter.
I needed to call getChildFragmentManager() on the Fragment, not getSupportFragmentManager() on the activity.
Thanks to these two posts for the answer:
Fragment in ViewPager not restored after popBackStack
and: Replacing ViewPager with Fragment - Then Navigating Back

Android - how to slide screens, inside a fragment

I've created an app, that has a main activity with a drawer menu so the user can click on some option and a fragment is open inside the activity.
What I'd like to do is to have several screens in one of the options and to navigate between them by tabs/slide the screen.
I saw an example of doing it inside one activity and with a fragment per 'sub-screen', my question is: how can I do it when I'm already 'inside' a fragment?
Thanks
Fragments can support having other Fragments inside them. The key to making this work is to remember to use getChildFragmentManager() to get the Fragment's FragmentManager instead of getFragmentManager() which gets the Activity's FragmentManager.
If you want to swipe between views, use a ViewPager inside your Fragment UI. The ViewPager will use a FragmentPagerAdapter to handle the Fragments for display.

Can i pass an Instance of a FragmentActivity into a Fragment transaction

I’m trying to do a tab layout, but before I’ve a navigation drawer that contains some menu. In one of those items I want to display a Tab activity or let’s say view. My problem is in the replace method we should pass a Fragment and in a tabbed view you can't do it with a Fragment you should have an activity or a FragmentActiviy. Now I’m trying to found a solution to this.
There is nothing that actually forbids using a ViewPager inside a Fragment, but I'd try to avoid going down that road. I fear for the UX, having both a navigation drawer and tab navigation is very confusing.
If you must do it nonetheless, you can find an example of using ViewPager on the official documentation page here, it is using Activity but the only main difference I see when implementing that in a Fragment is that the FragmentManager you use should be the one obtained calling getChildFragmentManager inside the main Fragment.

fragment inside a fragment in android

In my application I am making use of navigation drawer which is in the MainActivity and this navigation drawer has say 5 fragments. I am not maintaining any backstack of these fragments.
Now, the first fragment has one button which when clicked pushes a fragment (which I call an inner fragment). Here, I am maintaining a backstack because I want to get back to the first fragment from the inner fragment.
Now, I have a requirement in which I want to navigate from an activityA to the inner fragment.
Is this possible?
One way that I have thought of is to have the push code inside the first fragments create method (and make this conditional).
But I don't think its an appropriate way. Any suggestions would be helpful.

Categories

Resources