Introduction
- At the best of times the Android documentation can be overwhelming and confusing, at the worst of times it is very overwhelming and very confusing. This series hopes to boil down topics and make them less complex. For a full in depth understanding of the fragment lifecycle please check out the documentation which can be found HERE.
A quick note
- To get the most out of this post I recommend that you have a basic understanding of Fragments, Fragment Manager and Fragment transactions. I also want to stress again, this is a very simplified version of the Fragment lifecycle, so please read the documentation HERE if you want a deeper understanding.
Fragment Lifecycle
- Each fragment that gets created has it's own lifecycle. As the user navigates and interacts with the app, the fragment transitions through 5 possible phases.
1) INITIALIZED
2) CREATED
3) STARTED
4) RESUMED
5) DESTROYED
INITIALIZED
- when a fragment is instantiated, it begins in the
INITIALIZED
state. For a fragment to then transition through the rest of its lifecycle, it must be added to aFragment Manager
. The Fragment Manager is responsible for determining what state a fragment should be in and then moving it to that state. Once the fragment has been added to the Fragment Manager, the onAttach() callback is invoked to attach the fragment to it host activity. Remember that fragments can not exist on their own and must be hosted by either a activity or another fragment.
ExampleFragment newFragment = new ExampleFragment();//INITIALIZED
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.add(R.id.fragment_container_view, newFragment)
.commit(); //onAttach() is called after commit()
- From the code example above: when we create an ExampleFragment() instance the fragment enters the
INITIALIZED
state. We then begin a fragment transaction and add newFragment to the Fragment Manager. Once the transaction is complete, onAttach() is called and the fragment is ready to transfer to its next state in the lifecycle.
CREATED
- After the fragment has been added to the Fragment Manager and the onAttach() method has been called the fragment enters into the
CREATED
state. In this state callbacks like onCreate(), onCreateView() and onViewCreated() get called. This state is the appropriate place to do things like set up adapters on a RecyclerView. This is also the time that the fragment's View gets inflated(created)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// called after onAttach() and before onCreateView()
// is used to reinitialize previously saved data
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
// called after onCreate() but before onViewCreated()
// only used to manually inflate the fragment's View
// returns inflated view
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
// any extra work that involves the fragment's view is done in
// this method
}
From the code above: onCreate() is called right after onAttach() and before the other two methods. It is used for reinitializing previously saved data form the
savedInstanceState
state. onCreateView() is called after onCreate() but before onViewCreated(). It is in onCreateView() that you can manually inflate the view by using the LayoutInflater class. onViewCreated() is the last method called and it is where any view related work should be done. the view parameter of onViewCreated() is the inflated view returned from onCreateView().For most beginner applications you only need to really to understand the
CREATED
state and the methods it calls. If you have gotten this far and not created your own app, I would recommend that you try to build your own and release it to the Google play store. It will give you a much deeper understanding on how the lifecycle actually works.
STARTED
- Once the fragment enters this state, it is guaranteed that the fragment's view is available. Also, when the
STARTED
state is entered the onStart() method is invoked. It is also at this point that the fragment is visible to the user. the onStart() method is a good place to do begin drawing visual effects.
@Override
public void onStart() {
super.onStart();
Toast.makeText(getActivity(),"Now onStart() calls", Toast.LENGTH_LONG).show(); //onStart Called
}
- From the code above: we can call the onStart() method in any class that extends the Fragment class. If you run this code you should get a Toast stating,
Now onStart() calls
. Well this code doesn't do much, it does tell us that our fragment has entered theSTARTED
state.
RESUMED
- This state is entered when the fragment is visible to the user and all animation and transition effects have finished. This state signifies that the fragment is now ready for use interaction. It is also the state that triggers the onResume() callback.
@Override
public void onResume() {
super.onResume();
Toast.makeText(getActivity(),"Now onResume() calls", Toast.LENGTH_LONG).show(); //onStart Called
}
- From the code above: we can use the same code as the onStart() method but notice which Toast appears first.
Now onStart() calls
will appear first, followed byNow onResume() calls
which gives us visual conformation of a change of state. the state goes fromSTARTED
TORESUMED
.
DESTROYED
- If a fragment is removed from the back stack, or if the Fragment Manager is destroyed, the fragment's lifecycle enters the
DESTROYED
state. In this state the fragment then invokes the onDestroy() callback and signifying the fragment has reached the end of its lifecycle. - In order to demonstrate the
onDestroy() callback
you would need to create a more complex app with multiple fragments. but you can use the code below and just navigate around your app and see when the toast appears. The Toast popping up will signify the destruction of the fragment.
public void onDestroy() {
super.onDestroy();
Toast.makeText(getActivity(),"Now onDestroy() calls", Toast.LENGTH_LONG).show(); //onStart Called
}
Conclusion
- Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
Top comments (0)