I'm running into a problem with the `Navigation Co...
# android
b
I'm running into a problem with the
Navigation Component
and a ViewModel scoped to a nav graph ... I'm removing a few places in my app where I was using
onActivityCreated
in some fragments, since that callback is deprecated. I've been able to move most everything related to view work into
onViewCreated
, but I have a problem there because I need to do a few things with a nav graph scoped ViewModel. The problem is that when the containing activity is recreated (due to a configuration change, or process death, etc) the
onViewCreated
gets called before the containing Activity's
onCreate
is finished (in fact, it gets called as soon as the activity's
onCreate
is called). As a result, the Nav Graph has not been setup yet, and so it can't properly create the ViewModel that is scoped to the graph, and the app crashes. What's the path forward here? Per the deprecation notice in the docs for
onActivityCreated
, in the fragment's
onAttach
we should register a LifecycleObserver on the activity, observe for
on_create
and do work there. But that dooesn't work for me either. One of the things I need to do with the ViewModel is observe a LiveData, and the
viewLifecycleOwner
is not yet available (it's not available until the Fragment's
onCreate
) ... the problem I'm running into is that after a configuration change, the Activity's onCreate and the Frgment's onViewCreated are executed in parallel, as illustrated by @ppvi in this article: https://medium.com/androiddevelopers/the-android-lifecycle-cheat-sheet-part-iii-fragments-afc87d4f37fd#4e7a . As outlined there, the benefit of onActivityCreated is that it is guaranteed to be called AFTER the Activity's
onCreate
is finished.
i
What does the activity finishing
onCreate()
have to do with Nav Graph scoped ViewModels? Your NavController should absolutely already be created and available before any
onViewCreated()
calls when you're using Navigation 2.3.5
And it sounds like you're still using the
<fragment>
tag. You shouldn't be using that - using
FragmentContainerView
will mean your
onViewCreated()
will happen after
onCreate()
finishes anyways
b
Thanks @Ian Lake. I am indeed using the
<fragment>
tag. I went to switch over to the FragmentContainerView a while back and ran into some difficulties (I don't remember what exactly) . I'll look into that again. To answer your question, I'm setting the nav graph "manually" using
navController.setGraph
in my activity's
onCreate
... So, the
onViewCreated
in the fragment gets called as soon as the
super.onCreate
is called in my Activity (so, before the call to
setGraph
), and thus, the graph hasn't been set yet.
i
Yeah, you'll want to avoid the
<fragment>
tag then. There's a reason there's a lint check explicitly telling you not to use it for over a year
The docs all have been updated to use
FragmentContainerView
(note how to gets the
NavController
- that's how you need to do it in `onCreate()`: https://developer.android.com/guide/navigation/navigation-getting-started#navigate
b
Yep. Not sure what I ran into last time that made me not update then. Might have just been, "I don't have the me for this now, I'll get a defect filed and deal with it later", and well... I guess it's "later" now
Ah ha! I remember now! When I went to switch to
FragmentContainerView
before, this issue was still in it's infancy: https://issuetracker.google.com/issues/142847973 There was lively debate about what to do, and I thought, "I'll let the dust settle and then update" ...
and sure enough, I updated and everything works like a champ
so in the end .. RTFM ....
c
Happens to all of us! Glad it worked out!
118 Views