Can your non-null fields in `Fragments`/`Activity`...
# android
c
Can your non-null fields in `Fragments`/`Activity` ever have null values? Like when a fragment has been removed or an activity has been finished. I end up having stuff nullable, because even tho I can guarantee that an object has been initialised as part of the lifecycle, I'm unclear what happens when the fragment/activity is destroyed in terms of the instance of the class itself. Any reading material and opinions would be great
What do you do and for what cases? Is it
lateinit var
,
val
,
var something : SomeType? = null
?
c
You can now constructor inject with Fragments! https://developer.android.com/jetpack/androidx/androidx-rn#2018-nov
Per state though I prefer to store a single id in the savedInstanceState, and restore it with the
Bundle savedInstanceState
in
onCreate(...)
And sadly it is not possible atm to “guarantee that an object has been initialised as part of the lifecycle”
a
@Casey Kulm can't see it, which bit of that mentions fragment constructor injection?
c
@Alan Evans “You can now set a FragmentFactory on any FragmentManager to control how new Fragment instances are instantiated.”
“The androidx.fragment 1.1.0-alpha01 release now allows fragments to be constructed using a FragmentFactory with a FragmentManager instance” - https://medium.com/google-developer-experts/exploring-the-android-fragment-scenario-component-e369ec587419
👍 1
g
because even tho I can guarantee that an object has been initialised as part of the lifecycle
What kind object? Views can be nullable and you never can be sure that view is avilable, so better never cache them to avoid leaks. Or are you talking about some Fragment dependencies?