ConorG
02/23/2021, 10:39 AMDefaultLifecycleObserver
that can observe the entire Fragment lifecycle and not just the Fragment View Lifecycle?
I ask because I want to get my team on-board with using FragmentFactory. Some have pushed back citing that it’s awkward having to remember to set it before calling onCreate
and unset it in onDestroy
. I was able to somewhat alleviate the issue by creating a DefaultLifecycleObserver
that can unset it, and also register a FragmentLifecycleCallbacks
so in onFragmentCreated
I can check savedInstanceState
to determine if recovering from process death where I can set the required Factory again.
If I could hook into the Fragment’s onCreate
from the get-go I think it would be possible to just set the Factory once, like in an init
block, then the developer doesn’t have to remember to do any more maintenance on it.
@Ian Lake?Kenneth Leong
02/23/2021, 1:58 PMFragment.getLifecycle()
which lets you observe the fragment from onCreate()
onwards. The other Livecycle handle is Fragment.getViewLifecycleowner().getLifecycle()
and this is the lifecycle of the Fragment’s view only, and ends when the view is destroyed.Ian Lake
02/23/2021, 2:53 PMIan Lake
02/23/2021, 2:54 PMON_CREATE
event is too late to set a FragmentFactory. Your child fragments have already been restored by then with the default factoryIan Lake
02/23/2021, 2:54 PMIan Lake
02/23/2021, 2:56 PMIan Lake
02/23/2021, 2:58 PMinit
block. That's a perfectly valid place to set itConorG
02/23/2021, 3:40 PMwhat makes you think you have to unset the factory in onDestroy()? That’s never been a requirement.@Ian Lake Our project is modularised with many feature modules and a top-level Application module that binds them together. When I navigate from the app module to a feature module, the root node (Fragment) of that feature module will set a FragmentFactory scoped to that feature. When navigating back from that root feature to to the app module, I reset the FragmentFactory to be the default (or whatever one app-level uses). That’s what I use ON_DESTROY for. Otherwise the Framework is still trying to instantiate Fragments using the feature-scoped FragmentFactory.
Ian Lake
02/23/2021, 4:27 PMaddFeatureFactory()
and removeFeatureFactory()
method that swaps what feature specific factories it loops throughConorG
02/24/2021, 5:23 AMYeah I see it’s called after the super.onCreate so it’s just too late to suit my use case. For my delegate to work it would have to hook in earlier, like onAttach, which I’m guessing is impossible. The below snippet is what I currently use and it works, the fragFactory delegate also handles setting the factory again after process restore.eevent is too late to set a FragmentFactory.ON_CREATE
Ian Lake
02/24/2021, 6:27 AMinit
and 2) there's no need to do anything on destruction, since the FragmentFactory gets cleared out with the child FragmentManager when the Fragment is removedConorG
02/24/2021, 8:50 AMIan Lake
02/24/2021, 2:54 PM