I have an issue with my Android app that uses Navigation and ViewPager2.
I've the main Activity which implements a Navigation Drawer and has its own Navigation Graph and it works fine with the rest of the application.
Then I have a second Activity that has a ViewPager2 which displays 3 different Fragments. Now the problem is that I need to navigate from the Main Activity to the Pager activity keeping the original Navigation infrastructure. I know that each activity has it's own Navigation Graph, but the second activity has a ViewPager2 control so that means it has 3 Fragments and I cannot specify a startDestination in the new Navigation Graph .... because there are 3 and here is the problem. I can display the new activity but the Application Bar with the Back button which would navigate to the original activity is not displayed. Any solution ?
You can not handle fragment of viewpager through nav graph, but you can create parent fragment called ViewPagerTabsHolderFragment and inside it, you will set up the view pager adapter fragments then you can set the nav graph start destination to this ViewPagerTabsHolderFragment
Related
So i have a navigation drawer like so it has 3 buttons that go to 2 different fragments and the other button which goes to an Activity.
When I click one frag 1 my fragment opens up with the drawer still intact same goes for Frag 2
but when i click on the Activity 1 the Drawer disappears
but i would like the drawer to continue in the activity as well.
can this be done.
What you'd want to do (roughly) is
Establish a menu as one of your resources and establish the items in that list.
In the activity that you want to contain the drawer, create
the drawer object and create a callback for the onMenuItemSelected.
In that callback, reference the menu item ids you created before and use intents and fragment managers to either start the activity or fragment that you want based on what they select.
This can't be done.
The DrawerLayout lives within your Activity and the Fragments that you switch to also live within the same Activity. This is the reason why switching fragments will leave the drawer intact. It's because they both exist within the same Activity without any interference.
However, launching an Activity is different. This is a completely different Activity which has it's own layout.
You actually only have two options if you wish to continue using a Drawer for main navigation.
Remove the need for the second Activity and change that to a Fragment. This way, all your fragments will exist within the same parent Activity so it'll use the same drawer that exists in that parent Activity.
Create an identical DrawerLayout and NavigationView in the second Activity. Call code to have the Drawer be opened when it's created. This way, although you're not really using the same Drawer, you're giving the illusion that it's still the same Drawer.
I'm trying to set up navigation in my application, it works well for simple things but I can't get it to work for some of the clients requirements, what I'm trying to do right now is set up a navigation graph based on the one activity many fragment idea, unfortunately each of these fragments have their own sub navigation (requirement) so for instance my main activity hosts my main nav graph and swaps out fragments based on the navigation views menu's id's using the NavigationUI library, but the first fragment shown holds a bottom navigation view with just 2 fragments (don't get me started on why this is poor design) so I tried to give this fragment its own nav graph, this works in that it shows the home fragment but it doesn't allow me to navigate using said graph its always trying to get the main graph for the navigation view drawer regardless of the view I try to find it with, so I tried to nest a graph in the main graph which again works but this draws the fragment over my bottom navigation view making it impossible to see or press it, so my question is how would I control 2 NavigationUI components, my navigation view (drawer) and bottom navigation view? do I use 2 nav graphs or nest the nav graph? and then how do I get a handle on them as passing the view doesn't seem to work in this instance
So it was looking for the nav graph in the heirarchy which was missing my nav graph for this layout and finding the one in the main activity i found i could call a nav controller by its id from a fragment like this
NavController navController = Navigation.findNavController(requireActivity(), R.id.main_nav_host);
which means i could use the nav graph i wanted and solve my issue
I have 4 buttons on the main activity screen.
I also have a menu with 4 items and I want to show it using a navigation drawer. I described these 4 items as fragments and the fragment container were included in the navigation drawer activity.
What I want to do is reach these 4 fragments from the main activity, but in my main activity I can't reach the fragment container. What should I do? is there a solution to this problem?
I have a question about fragment management.
I have already programmed a drawer activity based on fragments. After every click on a menu item you will land on an empty fragment.
Now I want to have it so that no matter on which fragment I am on top the same is written. That's about 5 things that are there and depending on the action certain values on the fragment change whatever is there. Like a Information bar.
What options do I have and how do I do that?
Current project scope:
Main activity java + xml
and the Fragments + xml
The main activity is only for the Navigation Drawer. The app starts with a fragment.
How can I implement this?
in you main activity just commit which fragment you want to fill that first view with in onCreate.
I implemented common navigation drawer for all activities, and I implemented that.But now my problem is I have fragments as well as activities in my Navigation drawer,
I set
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
for all activities to restrict inflating same activity for twice but now I am unable to do the same for fragments of same Activity. If I add the above flag for fragments main activity other fragments are not getting selected how to solve these.
Thanks in Advance.