Can't Repro
Status Update
Comments
al...@android.com <al...@android.com>
ch...@google.com <ch...@google.com>
ki...@google.com <ki...@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().
ki...@google.com <ki...@google.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
ki...@google.com <ki...@google.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 :)
Description
In the end i have found that the strange behaviour is caused by the mutate issue of Transition/LayerDrawable that has been fixed on api level 18.
This is the code that i'm using to solve the problem:
private static Drawable getDrawableCompat(Resources res, int id) {
Drawable d = ResourcesCompat.getDrawable(res, id, null);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
// fix for "TransitionDrawable should not become a LayerDrawable"
//
// issue fixed on api 18+ (mr2) and that happens only on api 17 (mr1) because of the different
// drawable constantstate/mutate handling added on api 17
// "The drawables cache strikes again"
//
if (hasLayerDrawable(d)) {
d = d.getConstantState().newDrawable();
}
}
return d;
}
public static boolean hasLayerDrawable(Drawable d) {
if (d instanceof LayerDrawable) {
return true;
} else if (d instanceof DrawableContainer) {
final DrawableContainer.DrawableContainerState cs = (DrawableContainer.DrawableContainerState)d.getConstantState();
final Drawable[] children = cs.getChildren();
if (children != null) {
for (Drawable child : cs.getChildren()) {
if (child != null) {
if (hasLayerDrawable(child)) return true;
}
}
}
}
return false;
}