I'm really curious about how to determine (from an outer class) if a fragment's onCreateView()
has already been called. I have searched for similar questions but found none.
For instance, is fragment.isAdded()
a good indicator?
My first thought was simply fragment.getView() != null
, but I'm not 100% sure it would be reliable as it seems, and I'm also slightly reluctant to use it (for no particular reason, I just tend to avoid nullity checks). I would be happy to find a workaround. Suggestions I had:
isAdded()
Return true if the fragment is currently added to its activity.
This line is quite ambiguous in my opinion; added is not attached, but neither created. It might refer to FragmentTransaction.add()
(which is semantically wrong because you can have <fragment>
s stuck in your layout without having to call add
or replace
).
Still, FragmentTransaction.add()
documentation gives no info nor makes you think added -> created
. I'd say no.
isVisible()
Return true if the fragment is currently visible to the user. This means it: (1) has been added, (2) has its view attached to the window, and (3) is not hidden.
Looks good, in the sense that isVisible() -> isCreated
, but the third option makes it isCreated != isVisible
. I just think of fragments inside a view pager: not all are visible, but the fragments near the currently visible fragment are added, created and alive, you can call methods on them. But for them, isVisible() == false
. This is kind of too strict.
isInLayout()
Return true if the layout is included as part of an activity view hierarchy via the < fragment> tag. This will always be true when fragments are created through the < fragment> tag, except in the case where an old fragment is restored from a previous state and it does not appear in the layout of the current state.
I don't think this applies here.
getView() != null
Returns The fragment's root view, or null if it has no layout.
This still looks the one and only solution. I'd just like a confirmation about that.
Implement a callback
..to be called onCreateView()
or, better, onViewCreated()
. But:
getView != null
.Does Fragment.isAdded() imply that onCreateView has been called?
NO!! NO!! pause NOOOOOOO00000000000!!!!!
SIr
Fragment.isAdded()
is a notification that your Fragment
has been added to your Activity
, end of story.
The add()
method in FragmentTransaction
has 3 different methods, all adds Fragment
to an Activity
,and, two goes further to create your Fragment
s View
and attach it to a Parent
ViewGroup
by the aid of LayoutInflater
provided your first parameter is not 0 (id != 0)
To check if onCreateView()
has been called you need to override onViewCreated()
.
getView()
will always return null unless onCreateView()
is done
your solution is check Fragment.isVisible()
FYI: There is nothing wrong that i see with the documentation. Its pretty clear sir.
Hope i am lucid Sir
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With