'Clear Navigation graph start destination fragment
I've an old Activity where all the logic is inside the activity. I can't refactor that, But I want to add a navigation graph for new code and fragments.
My problem is that after I set the NavHostFragment with the navigation graph, I can't clear it and hide so I will see the activity's layout (like it stage 0 when the app starts)
The backstack is empty:
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val backStackEntryCount = navHostFragment?.childFragmentManager?.backStackEntryCount
if(backStackEntryCount == 0) {
Toast.makeText(this, "Back stack is empty", Toast.LENGTH_LONG).show()
}
so pop and navigate up don't work.
Any idea how can I clear the navHostFragment to initial stage, so the fragment will actually be destroyed?
Thanks.
Solution 1:[1]
With the AndroidX nav comp lib you should not clear fragments or the backstack yourself. Let the lib handle this. You could hide the NavHostFragment where the content fragments are displayed in. But I would try to refactor your old activity, because combining old and new navigation patterns is not a great idea.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
