https://kotlinlang.org logo
#android
Title
# android
c

ClaudiuB

11/28/2018, 1:03 PM
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

Casey Kulm

11/28/2018, 3:56 PM
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

Alan Evans

11/28/2018, 8:13 PM
@Casey Kulm can't see it, which bit of that mentions fragment constructor injection?
c

Casey Kulm

11/28/2018, 8:14 PM
@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

gildor

11/29/2018, 3:18 AM
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?
5 Views