1

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

SNM
  • 5,625
  • 9
  • 28
  • 77
  • Do you have any animations (i.e., `enterAnim`) attached to your action? Any kind of fade will make your Fragment semi-transparent so you'll be able to see your activity's background color underneath it – ianhanniballake Jul 22 '19 at 18:13
  • Hi Ian, yes, I have the default animations (nav_default_enter , nav_default_exit, nav_default_pop) – SNM Jul 22 '19 at 18:19
  • If I dont put the animations it also looks transparent above the activity, since the component is new, should I think to move to do it with FragmentTransactions or the problem is at my end ? – SNM Jul 22 '19 at 18:21
  • This is my flow, I have the main activity that holds all the fragments, with FragmentTransaction I inflate the first fragment ( Fragment A) and that Fragment A launches Fragment B, but Fragment B looks transparent above Fragment A – SNM Jul 22 '19 at 18:27
  • NavController only allows one Fragment at a time and does all the Fragment transactions for you. You shouldn't be doing any other transactions. – ianhanniballake Jul 22 '19 at 18:32
  • So I cant use 1 activity that hosts lets say 4 fragments ? Because I cant navigate through them – SNM Jul 22 '19 at 18:38
  • As long as you have only one at a time, a single NavHostFragment and having NavController swap between Fragments is exactly what you should be doing. – ianhanniballake Jul 22 '19 at 18:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196828/discussion-between-coffeebreak-and-ianhanniballake). – SNM Jul 22 '19 at 18:48
  • I have exactly how you mentioned, one NavHostFragment and then I navigate with NavController from each fragment, it navigates, but it does put my background transparent – SNM Jul 22 '19 at 18:53

1 Answers1

2

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443