Fixed
Status Update
Comments
il...@google.com <il...@google.com> #2
If you need to add a custom Navigator but don't want to subclass NavHost (which we expect will be needed in most cases), the correct approach is to add it before you set your graph:
1) Don't use app:navGraph in your XML
2) Get a reference to your NavController
3) Add your Navigator to the NavController
4) Call setGraph on the NavController
This relies on the fact that NavController waits until the graph is set before restoring state / doing anything at all.
Technically, the createFragmentNavigator() call in NavHostFragment is already called at the same time as your proposed onNavControllerCreated() method so as a workaround, you could override that method, add your Navigator, then return super.createFragmentNavigator().
1) Don't use app:navGraph in your XML
2) Get a reference to your NavController
3) Add your Navigator to the NavController
4) Call setGraph on the NavController
This relies on the fact that NavController waits until the graph is set before restoring state / doing anything at all.
Technically, the createFragmentNavigator() call in NavHostFragment is already called at the same time as your proposed onNavControllerCreated() method so as a workaround, you could override that method, add your Navigator, then return super.createFragmentNavigator().
ru...@gmail.com <ru...@gmail.com> #3
The first solution does not work, because after screen rotation, the graph (probably a restored graph) is set when Activity.setContentView() is called.
The workaround with createFragmentNavigator() works, though it's not very elegant. Maybe the method could be renamed to createNavigators() and return a list or an array of Navigators?
Anyway, it is a good enough solution for now, so it is not very urgent.
In conclusion:
* I think that a better support for custom destinations should be provided before final release.
* I would suggest describing the "Add support for new destination types" section in the documentation in more details.
Thank you
The workaround with createFragmentNavigator() works, though it's not very elegant. Maybe the method could be renamed to createNavigators() and return a list or an array of Navigators?
Anyway, it is a good enough solution for now, so it is not very urgent.
In conclusion:
* I think that a better support for custom destinations should be provided before final release.
* I would suggest describing the "Add support for new destination types" section in the documentation in more details.
Thank you
ru...@gmail.com <ru...@gmail.com> #4
I suspect the issue you're running into for the first solution is that you're using the setGraph(R.navigation.graph) method, which does indeed cause it to automatically inflate the graph by id on rotation. Using setGraph(navGraph) where navGraph is a navGraph object you've inflated with navController.getNavInflater().inflate(R.navigation.graph) will never automatically be recreated.
Definitely something we can do better on though :)
Definitely something we can do better on though :)
ru...@gmail.com <ru...@gmail.com> #5
Project: platform/frameworks/support
Branch: androidx-master-dev
commit a2e68921841727237528c4ac7076d50af64663c8
Author: Ian Lake <ilake@google.com>
Date: Wed Oct 17 16:46:28 2018
Make setGraph(int) and setGraph(NavGraph) act identically
Remove the auto restore functionality from NavController's
setGraph(int) method so that it functions identically to
setGraph(NavGraph).
This ensures that developers that manually are calling
setGraph() after adding custom navigators don't get
recreated too early.
Test: updated NavController test
BUG: 110763345
Change-Id: I51ae7c74451669d6bfed0fe84df445c2ff272e6e
M navigation/runtime/src/androidTest/java/androidx/navigation/NavControllerTest.kt
M navigation/runtime/src/main/java/androidx/navigation/NavController.java
https://android-review.googlesource.com/792819
https://goto.google.com/android-sha1/a2e68921841727237528c4ac7076d50af64663c8
Branch: androidx-master-dev
commit a2e68921841727237528c4ac7076d50af64663c8
Author: Ian Lake <ilake@google.com>
Date: Wed Oct 17 16:46:28 2018
Make setGraph(int) and setGraph(NavGraph) act identically
Remove the auto restore functionality from NavController's
setGraph(int) method so that it functions identically to
setGraph(NavGraph).
This ensures that developers that manually are calling
setGraph() after adding custom navigators don't get
recreated too early.
Test: updated NavController test
BUG: 110763345
Change-Id: I51ae7c74451669d6bfed0fe84df445c2ff272e6e
M navigation/runtime/src/androidTest/java/androidx/navigation/NavControllerTest.kt
M navigation/runtime/src/main/java/androidx/navigation/NavController.java
il...@google.com <il...@google.com>
ap...@google.com <ap...@google.com> #6
We've decided to remove the automatic recreation that was unique to setGraph(int) - it now acts identically to setGraph(NavGraph), so the flow in #2 can be used to ensure that you have custom Navigators added before calling setGraph.
This fix will be available in 1.0.0-alpha07
This fix will be available in 1.0.0-alpha07
il...@google.com <il...@google.com> #7
Project: platform/frameworks/support
Branch: androidx-master-dev
commit f40bad0faef05bded2d226711338e489309885ba
Author: Ian Lake <ilake@google.com>
Date: Tue Oct 23 21:03:24 2018
Ensure NavHostFragment calls setGraph on restore
If a graph is set on NavHostFragment via
NavHostFragment.create() or through
app:navGraph, it should be restored immediately
after the call to restoreState.
Test: testapp now works
BUG: 110763345
Change-Id: I7a0c656480e27a66637667519eabe61820b09a6c
M navigation/fragment/src/main/java/androidx/navigation/fragment/NavHostFragment.java
https://android-review.googlesource.com/799721
https://goto.google.com/android-sha1/f40bad0faef05bded2d226711338e489309885ba
Branch: androidx-master-dev
commit f40bad0faef05bded2d226711338e489309885ba
Author: Ian Lake <ilake@google.com>
Date: Tue Oct 23 21:03:24 2018
Ensure NavHostFragment calls setGraph on restore
If a graph is set on NavHostFragment via
NavHostFragment.create() or through
app:navGraph, it should be restored immediately
after the call to restoreState.
Test: testapp now works
BUG: 110763345
Change-Id: I7a0c656480e27a66637667519eabe61820b09a6c
M navigation/fragment/src/main/java/androidx/navigation/fragment/NavHostFragment.java
ru...@gmail.com <ru...@gmail.com> #8
Thank you for fixing this. Would singleDialog
have to be also used in the navigation resources files? I didn't know we could create custom navigation targets with different names outside of dialog
, fragment
and activity
il...@google.com <il...@google.com> #9
The default DialogFragmentNavigator
, FragmentNavigator
, and ActivityNavigator
use the exact same @Navigator.Name
API as is available to every custom Navigator (there's no special casing on the Navigation library side), so yes, it can also be used from XML.
Description
Version used: 2.3.2
Caused by:
- NavHostFragment looks up a DialogFragmentNavigator in "onAttachFragment" and no null check is done
- A custom Dialog fragment navigator can't extend from DialogFragmentNavigator as it's a final class
Commit that introduced this:
Can the new API be used instead to fix this issue in the next alpha/beta release?
In alternative, do you suggest other workaround or should we wait and stay in 2.3.1 until this is fixed?