Issue
onActivityCreated
seems to mean "This fragment callback is executed just after the activity has been... created... I mean, just after the fragment is correctly attached to the activity. There, you can safely call getActivity
, it won't return null, except if the activity is null
for some special reason".
However, I've seen that the fragment callback onAttach
is called even after OnCreate
, it means that the fragment has been attached to the activity, which has been created.
The complete workflow for a fragment (and for a fragment dialog, which is frequently left behind) is: onAttach
-> onCreate
-> onCreateView
-> onActivityCreated
. So in each of these 4 methods (perhaps not onAttach
I don't know), the activity is normally not null and attached to the fragment.
So my question is: why does the callback onActivityCreated
since the activity is, in fact, already created and attached to the fragment 3 callbacks ago???
Solution
So basically in onAttach() we get confirmation is that activity is attached to my fragment, I can use getActivity() to fetch things like resources like
getActivity().getResources.getDrawable(R.drawable.abc)
but Suppose if You want to fetch the views inflated in activity's xml like If you want to access
getActivity().findViewById(R.id.Myelement)
You might get null here, so OnActivtyCreated() ensures that activity's view is inflated, You can access activity's views now (Activity's view has been created (onActivityCreated))
Answered By - akashzincle
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.