Status Update
Comments
il...@google.com <il...@google.com>
jb...@google.com <jb...@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.
yo...@gmail.com <yo...@gmail.com> #3
jb...@google.com <jb...@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
yo...@gmail.com <yo...@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>
ja...@gmail.com <ja...@gmail.com> #6
Is there a planned fix for this issue? We've been stuck on Navigation 2.5.3 for some time due to issues in 2.6.x and now this issue, and the suggested workaround of restructuring our navigation graph is a pretty big lift. If that's the only recommended path it would be helpful to know so we can try to plan for that work, otherwise we'd want to just wait for the fix that doesn't require restructuring.
jb...@google.com <jb...@google.com> #7
This was fixed by
ap...@google.com <ap...@google.com> #8
Branch: androidx-main
commit b292676aa37122f0ca0215023dcdbe27168b2469
Author: Jeremy Woods <jbwoods@google.com>
Date: Wed Sep 27 21:25:35 2023
Add a test for navigate with popUpTo nested graph
Adding a test to ensure that if the start destination of a nested graph
is popped off the back stack when a fragment is created, Navigation does
not crash and gets to the correct position.
Bug: 287133013
Test: adding a test
Change-Id: Ie1e03aeeb00b730b7688311741847b2312bdbaa0
M navigation/navigation-fragment/src/androidTest/java/androidx/navigation/fragment/NavControllerWithFragmentTest.kt
na...@google.com <na...@google.com> #9
The following release(s) address this bug.It is possible this bug has only been partially addressed:
androidx.navigation:navigation-fragment:2.8.0-alpha01
Description
Version used: navigation-fragment-ktx, navigation-ui-ktx 2.6.0
Devices/Android versions reproduced on: All
If this is a bug in the library, we would appreciate if you could attach:
- Sample project to trigger the issue in the following github link:
When I open an app by the deep-link and back-pressing, it crashes. Also, bottom navigation tab 2 has dynamic fragment logic. It will change fragments when the API has a data response. (A sample project doesn't use the API.)
FATAL EXCEPTION: main
Process: com.example.navigationdeeplink, PID: 21948
java.lang.IllegalArgumentException: The fragment SecondFragment{389b31b} (e54e1f09-0f45-4e6c-a668-35e52c4c6ffc id=0x7f0800cc tag=cf5dd8f1-9576-4c91-975d-73e99d7e34b2) is unknown to the FragmentNavigator. Please use the navigate() function to add fragments to the FragmentNavigator managed FragmentManager.
at androidx.navigation.fragment.FragmentNavigator$onAttach$2.onBackStackChangeCommitted(FragmentNavigator.kt:143)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1918)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:565)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7884)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Issue does not occur when downgrade version 2.6.0 to 2.5.3 or normal open an app
Steps to reproduce
1. run script deeplink (file name test_deeplink.sh in a project)
2. press back system
3. App crashes with the above error