Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why we can call getActivity() in onCreateView which run before onActivityCreated?

I really get confused with Fragment lifecycle, especially for the time to call getActivity(). Sometimes you cannot get Activity by getActivity(). And it always caused some puzzling bugs.
Thank you for anyone can solve the puzzle.

like image 445
CodeAlien Avatar asked Jul 06 '15 00:07

CodeAlien


People also ask

Is onAttach called before onCreateView?

Bookmark this question. Show activity on this post. In a Fragment's Lifecycle, the onAttach() method is called before the onCreate() method.

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

Is called after the onCreateView () method when the host activity is created?

onStart() : is called after the onCreateView() method when the host activity is created.

What is getActivity ()?

getActivity() in a Fragment returns the Activity the Fragment is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).


2 Answers

getActivity() can be null while your fragment is in process of preparation and about to be ready.

The fragment life cycle is bound to callback methods. These method will be called somewhere in time while fragment is preparing.

  • Fragment.onActivityCreated(Bundle) is the place when the fragment activity will not be null, i.e. getActivity() will be a valid instance. It happens after onCreateView() though

Your safest bet for activity existence is:

  • Fragment.onViewCreated(View, Bundle)
  • Fragment.onStart()
like image 137
Dimitar Genov Avatar answered Oct 22 '22 15:10

Dimitar Genov


According to the current documentation (Dec 2018), it shows that onAttach() is called right at the beginning before onCreate() and onCreateView(). It should be safe to getActivity() in these methods.

Fragment lifecycle


In the Support v4 Fragment documentation for onActivityCreated() it says that this method is:

Called when the fragment's activity has been created and this fragment's view hierarchy instantiated.

The important part here is that the "activity has been created" i.e. Activity.onCreate() has finished executing. Before this point we are still within that method.

This can be confirmed by looking at the FragmentActivity.onCreate() source code you can follow the process of fragments being attached to the activity at the start of the method, then the fragment state being restored etc etc. So the activity should be valid in all those places, but technically it has not finished with the whole create process.

like image 26
Richard Le Mesurier Avatar answered Oct 22 '22 13:10

Richard Le Mesurier