Navigation Component is not replacing current fragment activity - android

So, I'm trying to navigato from one Fragment to another, I have used this line
val action = MapsFragmentDirections.nextAction(marker?.title!!)
findNavController().navigate(action)
this navigation goes to another Fragment, But in the time it reachs that fragment, I see the activity below my fragment and my fragment is transparent with the recyclerview above.
I have researched and found that I need to se replace with FragmentTransaction, but I think that is done in the background of Navigation Component.
The problem is that when inflating my other fragment, I can see my activity below the view of the fragment.
A temporary fix is to put the background of the RecyclerView as blank, but I dont want to fix it that way, I want it to be inflated like a normal fragment without seeing nothing under it
My problem is exactly this one : Fragment is transparent and shows Activity below
But I need to solve it with Navigation Component, and I dont see any method that helps me do that.
Or does I need to set all my view inside a fragment> tag in my xml ?
Thanks

When using NavController, you shouldn't be doing any separate FragmentTransactions at all - as long as you've added a NavHostFragment to your XML and are using the app:navGraph attribute to specify your graph, the NavController does all the work on switching between Fragments and making sure they don't overlap.

Related

Move to one fragment to another fragment of different container activity

I have an container activity which will contain the fragments which will be added to this container when my bottom nav bar is navigating. Now I want to go to one of this fragment to another fragment which will replace another different activity by set on click listener of a cardview.
I am new fragment so I dont understand how to do that. Hope someone will help me.
You approach is wrong. You cannot navigate from one activity fragment to another activity fragment. The correct way will be to have single activity with multiple fragments inside. Using Navigation component will be the best way of navigation.

How to setup Multiple nested FragmentContainerViews with respective navigation graphs?

I was looking into converting my app to single activity as per google's guidelines. (Using Kotlin)
Let's say i have FragmentContainerView1 in my activity with no toolbar or anything else.
This view is used to navigate between login screen,sign up and display the main fragment.
Inside the main fragment, I have another FragmentContainerView2 which should display according to a BottomNavigation view inside main fragment.
But then i have some elements inside the fragments in FragmentContainerView2, that need to perform actions in FragmentContainerView1.
How do i achieve this?
Picture :
Here mainFragment contains the BottomNavigation,of which mainMenuFragment is a destination,which has to perform actions in FragmentContainerView1. How can I get a reference of the inner nav controller of the BottomNavigation?
Edit 1: navController=Navigation.findNavController(view.findViewById(R.id.fragmentContainer)) doesn't work, still returns same outer nav controller.
Not a Complete Solution, But the answer I found was to keep one of the containers as a <fragment/> and the other as FragmentContainerView.
And to get the inner nav controller use:
innerNavController= (activity as AppCompatActivity).findNavController(R.id.innerFragmentContainer)
Only works with that specific command and one of the containers being <fragment/>.
Also after getting the innerNavController, Any type of call with findNavController() gives the inner nav controller.
To get the outerNavController use:
outerNavController = (activity as AppCompatActivity).findNavController(R.id.outerFragmentContainer)

Understanding android navigation drawer and fragments

i'm quite new to drawer material and i have trouble understanding some things:
I need to create an Activity with a Fragment on it. Different selections on the Drawer must replace the current Fragment with another one, but is the Drawer something in the fragment or it is one for the activity itself. More specifically is the Drawer living in the Fragment and if not is it possible to create it in the Fragment. I am asking this cause i see that when initiating the Drawer you need to fill parent activity. Also when i tried to use the Navigation Drawer template in Android Studio i didn't had the Use a Fragment checkbox.
There are many ways to achieve what you want. But I think the simplest way is:
1) DrawerLayout view should reside in the activity (probably as the base layout).
2) When you click on an item in the draw 2 things happen:
The fragment is replaced (you have one layout to contain the fragment and you just replace the fragment in it).
The items inside the drawer update (if you are making a list you would simply set the data and call notifyDataSetChanged().
Don't forget to save your state so it can recover in case the Activity is recreated.

Closing FrameLayout for a Fragment in Android App

I'm working on a simple Android App as a self-learning Project. I've got a lot of it functioning, and I have a main Activity which has a FrameLayout and some RecyclerView and FloatingActionButton stuff going on inside of it.
However, I want to make one of my buttons in my NavigationDrawer open a different view in the FrameLayout using Fragments. Is there a way to do this, sort of making a new Fragment for the RecyclerView and the other stuff and putting the RecyclerView and FloatingActionButton in there?
I tried doing something like this (when the appropriate NavigationDrawer button was clicked):
statsFragment = new StatsFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.rootLayout, statsFragment);
transaction.addToBackStack(null);
transaction.commit();
But this caused my app to crash. Any pointers?
Is your R.id.rootLayout a Layout or a Fragment component?
If your main Activity has a hard coded Fragment (by hard coded i mean declared in your XML), then no, you can't change a hard-coded fragment content with FragmentTransaction.
More information on this question's answer.
Finally, you can't put your components from one Activity inside a new Fragment/Activity since each component belongs to one and only one Context.
See, Activity extends Context. When you get a reference to a component declared in your XML by using Activity.findViewById(int), you are actually making a pointer to a determined View from this Activity's layout, defined in your onCreatemethod with setContentView(id). You can't take this pointer and throw it to another Fragment or Activity within your application without expecting any problems. (Well, maybe you can, but it's totally against the best practices of Android Programming).

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.

Categories

Resources