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

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

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.

BottomNavigationView in child fragment in Navigation Component

I want to achieve something like this image flow in android navigation component. Where the Dashboard Fragment is the start destination. And from here i can navigate to another fragment which have a bottom navigation view. Is this possible using a single nav graph and a single activity? What is the best way to achieve something like this?
You can use BottomNavigationView
val childNavView: BottomNavigationView = binding.nav1View
val navController = NavHostFragment.findNavController(childFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment)
childNavView.setupWithNavController(navController)
In case someone's still looking for a solution.
Just use two separate navigation graphs. And when navigating, pick the right navigation controller object. You can use a single activity of course, with nested fragments. So your "Bottom nav page" will have its own navigation container with child fragments in it.
Moreover, you can also navigate from the inner nodes to outer ones. In that case you need to reference the parent fragment ("Bottom nav page") from the child fragment and then obtain its navigation controller like this:
parentFragment?.parentFragment?.findNavController()
Here the first parentFragment is NavHostFragment and its parent is your actual parent fragment ("Bottom nav page").
If you want Page 1-3 to have options for further fragments to navigate inside (i.e. Page 1.1, 1.2 for Page 1, etc), then you need a navgraph for each Page (so 3 total), I would also make them all the root of their own navigation, would make it a lot easier to implement, and just replace the dashboard fragment with an activity if it's for signing in for example.

How to save sate of bottom navigation view fragments with use of new android jetpack navigation?

I have a problem with saving the state of fragments which work with bottom navigation view. I am using the new android jetpack navigation library and it is really simple to code bottom navigation with fragments.
I have 3 fragments in the bottom navigation. When I switch, the fragments are recreated.
OnSaveInstanceState is never called when I just switch between fragments, so it does not work.
I tried ViewModel too, it works but I lose the recyclerview position when I switch.
So I just want to simply save the state of fragments with recyclerviews. I don't understand why this simple problem is so difficult to solve. Is there any proper way how to achieve this? I have done a lot of research but I have not found a solution. Thanks.

What are the advantages of using Navigation Drawer with Activities VS with Fragments?

I want to implement a Navigation Drawer in my app but I am conflicted on whether I should use it with Fragments or with Activities (see image below for more details).
Is there any real advantages or disadvantages between the two or is it just a matter of preference?
Edit:
Just to clarify my question:
In the case of using Activities instead of Fragments;
When I select "Import" that will open an Activity and not a Fragment and if I select "Gallery" it will open an Activity with contents for gallery item etc. and so on for the other items in the Drawer window.
In the case of using Fragments instead of Activities;
If I choose from any of the Items in the Drawer window it will open their contents in Fragments for each Item selected instead of starting new Activities for each selction.
Remember Fragments need an Activity. You always have one minimum when using Fragments.
If you are talking about to use like main element in the most cases is best use fragments because you have more flexibility UI.
The performance would be better if you have 3 activities and 10 Fragments or have 13 Activities? Think about it, the navigation within the App would be the big challenge but it's just about using the right flow in your application.
Edit:
For instance:
Drawer With Activities instead of Fragments
If you were to use NavigationDrawer without Fragments then it would be best to keep the NavigationDrawer instance in a single Activity and when you navigate the app by choosing from the items in the NavigationDrawer then each of those Activities that are started should not implement the NavigationDrawer but instead should implement the back button to Navigate back to the "Main"/single Activity that the NavigationDrawer was implemented in.
Note: If you do want to implement the NavigationDrawer in multiple Activities you would have to recreate a new instance of the NavigationDrawer in every Activity that you desire to display it.
I suppose this would be a disadvantage vs using Fragments whereas if you used a fragment you wouldn't need many instances of the drawer you would only need one.
Drawer With Fragments instead of Activities
If you use the NavigationDrawer with Fragments then the drawer should be implemented in a single Activity and when each drawer item is selected, their contents are displayed in each of their very own Fragments(which is called inside of the central Activity which manages all the Fragment instances)

Using ViewPager and PagerTabStrip - extending FragmentActivity

First - sorry for the newbish question.
I've started building an app that has a single Activity and a navigation drawer. Each menu item loads a new fragment in the middle frame layout.
I want to create another fragment that:
has tabs
allows for swipe scrolling
It seems like the only way to do this is to create add a ViewPager and PagerTabStrip. All the tutorials I've read indicate the ViewPager requires extending out to FragmentActivity. I have a few questions:
Am I doing anything wrong by replacing the fragment content when navigating menu options?
If what I am doing is ok, is there anyway to incorporate swipe navigation without calling FragmentActivity?
If I need to use FragmentActivity for this one page, I'm assuming I'll call change pages via Intent. Doing so would result in losing the click actions in the navigation drawer. Do I have to call (or duplicate) my code from one activity to another?
EDIT
Sorry about the confusion. Here's what I'd like my application do:
Open app. MainActivity starts. Navigation drawer loads. Main content is loaded via a fragment.
User opens navigation drawer and selects this new menu item I'm creating. It is a new fragment that loads in the frame (like the other menu items). However, it has tabs and supports swiping.
ViewPager is just usual descendant of View so it can't require using of FragmentActivity.
It's absolutely ok.
You don't need to use FragmentActivity. I suppose you just read tutorial about "Implementation of drawer" where author of the tutorial used FragmentActivity.
Can't understand what do you mean. Pages of ViewPager is just views not activities. You don't need to use Intent.
PS Actually I can't understand your problem at all. It's absolutely unclear why you don't want to use FragmentActivity.

Categories

Resources