What would be better approach to send data between fragments inside of ViewPager 2, and their host fragment? - android

I have an app with few fragments. There are one fragment, which is host for ViewPager 2, and there are 4 fragments, that are "swipeable". There's one more fragment, which is not present in ViewPager. It is accessible via NavigationComponents.
These fragments, that are using ViewPager, has it owns toolbar. I'd like to send fragment's TAG to the host fragment, so it will know, where to redirect ViewPager later on. After receiving the TAG, it should move to fragment, which is accessible only via Navigation Components.
The problem is, I'm not sure how I should approach it. Should I create a ViewModel for host fragment, and there should I set LiveData with TAG, or should I make an Singleton object inside the host fragment, send the TAG from fragment inside of ViewPager, and then, trigger Navigation Components?
I need to pass the TAG of the fragment inside of ViewPager, because I want program to know, where it should back after some stuff done in fragment from Navigation Components.

Related

Error while calling Navigation Components while using ViewPager 2

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.

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.

Android: Different fragment for each ViewPager

In Android, when creating Action Bar Tabs with ViewPager, what's a way of giving different fragment for each ViewPager? Let's say I want the first tab to be login form, the second a signup, in fragment_login.xml and fragment_signup.xml files respectively.
Where/how do I initialize these fragments and show as appropriate tabs are selected? I would prefer to do this all in one Fragment class, instead of creating individually for each one.
If you are using a small number of fragments then you can implement FragmentPagerAdapter. For showing larger number of fragments FragmentStatePagerAdapter is recommended. You can keep all the fragments in one class(Activity class) and make each fragment a subclass of that class but I think having different classes in respective .java files would make your code more elegant.
Initializing the fragment is generally done during fragmentTransaction and appropriate data are passed via Bundle.

LoaderManager on Activity with multiple Fragments (on ViewPager)

I have an Activity which hosts 4 Fragments on a ViewPager. Each of these Fragments calls getLoaderManager().initLoader(0,null,this).
The documentation says the first parameter is an ID. So I find myself wondering, the fact that I use 0 as the id for every fragment inside the same ViewPager does it affect performance?

Fragment inside a fragment with ViewPager

Is it possible to put a fragment inside a fragment in Android? Consider this: Say, I implement swipeable tabs using Fragment and ViewPager. Now, inside each of these swipeable fragments, I want to implement another fragment - kind of like a fragment nested inside another fragment. But a fragment needs to be attached to an Activity class. So how can this be done?
Is it possible to put a fragment inside a fragment in Android?
Using the Android Support package's backport of fragments, yes. Also, native fragments on Android 4.2 and higher (API Level 17+) support nested fragments. However, native fragments from API Level 11-16 do not.
Say, I implement swipeable tabs using Fragment and ViewPager. Now, inside each of these swipeable fragments, I want to implement another fragment - kind of like a fragment nested inside another fragment. But a fragment needs to be attached to an Activity class. So how can this be done?
This sample project demonstrates having fragments in a ViewPager and having the ViewPager itself be in a fragment. The key is that the FragmentManager you supply to your FragmentPagerAdapter must be the child fragment manager of the outer fragment (i.e., getChildFragmentManager()).

Categories

Resources