Status Update
Comments
ki...@google.com <ki...@google.com>
al...@google.com <al...@google.com> #2
So there does seem to be a bug in Navigation that causes this to fail.
When it navigates with popUpTo
and removes SecondFragment
, that removal in fragment is set for a frame later and before it is actually removed the navigate call in the onViewCreated
of SecondFragment
enqueues another call to add it back. But we still need to destroy SecondDetailFragment
which ends up marking the NavBackStackEntry
associated with SecondFragment
as complete before navigation has received the call from the fragment that it has been added back. So by the time it hits this check, the SecondFragment
is in the correct state, but Navigation has no way of referencing it so it assumes that it is not associated with an entry.
But this is not the way this type of situation should be implemented. Instead of having the destinations as part of the NavGraph, they should:
- be managed by the childFragmentManager of
SecondFragment
. So instead of going back toSecondFragment
just to go somewhere else, each of the fragments take the entire content ofSecondFragment
and are swapped out. - for some sort of a/b testing, set different graphs in the Activity. The graphs would keep the same
SecondFragment
id, but swap out the names and then you choose the correct graph based on the condition.
e....@gmail.com <e....@gmail.com> #3
al...@google.com <al...@google.com> #4
- Have
SecondFragment
as part of your graph and then inside ofSecondFragment
call thechildFragmentManager
andreplace
with the fragment you actually want to show, whether that isSecondTypeOne
orSecondTypeTwo
. - Create different graphs and follow along with the
on using different graphs dynamically and inflate and set the one needed based on logic in your Activity.docs
pe...@gmail.com <pe...@gmail.com> #5
shardViewModel.onGetTypeSecond.observe(viewLifecycleOwner) { isType1 ->
if (isType1) {
val graph = findNavController().graph
val carInstallmentNavGraph =
graph.findNode(R.id.nav_second) as NavGraph
carInstallmentNavGraph.setStartDestination(R.id.secondTypeOneFragment)
findNavController().navigate(SecondFragmentDirections.actionSecondFragmentToSecondTypeOneFragment())
} else {
val graph = findNavController().graph
val carInstallmentNavGraph =
graph.findNode(R.id.nav_second) as NavGraph
carInstallmentNavGraph.setStartDestination(R.id.secondTypeTwoFragment)
findNavController().navigate(SecondFragmentDirections.actionSecondFragmentToSecondTypeTwoFragment())
}
}
and removed app:popUpTo="@id/secondFragment" and app:popUpToInclusive="true"
<fragment
android:id="@+id/secondFragment"
android:name="com.example.navigationdeeplink.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
<action
android:id="@+id/action_secondFragment_to_secondTypeOneFragment"
app:destination="@id/secondTypeOneFragment" />
<action
android:id="@+id/action_secondFragment_to_secondTypeTwoFragment"
app:destination="@id/secondTypeTwoFragment"
app:popUpTo="@id/secondFragment" />
</fragment>
Description
Version used: 1.2.0-rc01 and 1.3.0-alpha01
Devices/Android versions reproduced on: Emulator Pixel 3 API 29, Emulator Pixel 2 API 28, Samsung S9 API 29
Calls to AppCompatDelegate.setDefaultNightMode() don't work well on the latest AppCompat versions, but it works on the latest stable version.
Most of the time, the theme doesn't change.