Is it safe to use this constructor `Fragment(int ...
# android
p
Is it safe to use this constructor
Fragment(int layout)
I'm getting couldn't find fragment constructor, I'm suspecting this guy.. 😭 But I also see it's call super(). I have
HomeFragment
which extends the
BaseFragment
. Is that because of that .
i
Your class that extends
Fragment
can use the
Fragment(int layout)
constructor in its zero argument constructor to set the default layout.
Or you can have
BaseFragment(@LayoutRes layout: Int = 0)
and
HomeFragment : BaseFragment(R.layout.fragment_home)
r
I have given up on “base” fragments.
💯 4
I just extensions if need be.
i
The docs explicitly tell you how you can use that constructor: https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#init_1
p
screen_shot_2020-11-03_at_10.24.20_am.png,screen_shot_2020-11-03_at_10.24.32_am.png,screen_shot_2020-11-03_at_10.24.49_am.png
We need a default value
That's the problem @Ian Lake
Then when Fragment restoration happens, how exactly my UI going to looks like? 🤭 We are defaulting to 0
i
As long as the fragment you're adding to the FragmentManager is passing a non-zero layout ID from its zero argument constructor, that's what you'll be using every time that fragment is created
Since you never add a plain old
BaseFragment
to the FragmentManager (but only subtypes), it doesn't matter if
BaseFragment
has a zero argument constructor
👍 1
p
How can I test my fix?
adb shell am send-trim-memory or logcat terminate application?
@Ian Lake
How do you test that usually 😌
i
Rotate your device? Turn on 'Don't keep activities'?
p
I was over thinking .. let me try 🤭
Not able to reproduce .. it didn't crash
😭
i
If your
HomeFragment
has a zero arg constructor like the image you shared has, no, that won't crash, no matter what your
BaseFragment
does or what constructor it has or calls
p
Then why did I got this crash
Couldn't find fragment constructor this is the exception
I'm getting if I go with what is there in screenshot
I was thinking because of we are passing layout in the Fragment constructor
i
Clearly there is a zero argument constructor since you call
HomeFragment()
p
I decompiled it .. I did see a public constructor ..👍
How can I fix this crash?
How can I reproduce this crash?
I'm lost in the woods
i
Fragments is a deterministic system - it either always crashes with the same inputs or never crashes. A fragment with a public zero argument constructor can't fail with an error saying there is no public zero argument constructor 🤷
p
@Ian Lake what about custom view in a layout .. will that cause any issues?
i
That seems unrelated to fragments, fragment constructors, or state restoration of fragments
p
Why I thought like that because, that also has overloaded constructors .. anyway thanks @Ian Lake you have so far been very helpful 😎👍🙏
i
I hope you're able to figure out what went wrong / reproduce it
p
Today was a tiring day 🤣 I'll check that tmrw