Bradleycorn
04/08/2021, 9:48 PMNavigation 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.Ian Lake
04/08/2021, 10:04 PMonCreate()
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.5Ian Lake
04/08/2021, 10:04 PM<fragment>
tag. You shouldn't be using that - using FragmentContainerView
will mean your onViewCreated()
will happen after onCreate()
finishes anywaysBradleycorn
04/08/2021, 10:43 PM<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.Ian Lake
04/08/2021, 10:45 PM<fragment>
tag then. There's a reason there's a lint check explicitly telling you not to use it for over a yearIan Lake
04/08/2021, 10:46 PMFragmentContainerView
(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#navigateBradleycorn
04/08/2021, 11:13 PMBradleycorn
04/09/2021, 1:18 AMFragmentContainerView
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" ...Bradleycorn
04/09/2021, 1:19 AMBradleycorn
04/09/2021, 1:19 AMColton Idle
04/09/2021, 2:28 AM