A fragment is a reusable component of the application user interface. It is also known as the sub-activity. Therefore the fragment lifecycle in android is dependent on the activity which inflates it.

Need some career advice or prepping for an Android developer interview? Hit me up on Topmate.io, and let's chat!

fragment lifecycle in android
fragment lifecycle in android

A fragment has its individual XML and java file which is used to handle user input events. Fragment is completely dependent on the activity.

It can be only hosted within an activity or a fragment itself. A fragment cannot live on their own; they have to be attached with the host’s view hierarchy. Fragments can be added, removed or replaced dynamically within the activity. As the fragment is completely dependent on the activity its lifecycle is also dependent on the activity.

fragment lifecycle in android
fragment lifecycle in android

If the host activity in which we are displaying is paused then all the methods used within the fragment are also paused as the lifecycle of the fragment is dependent on the activity.

There are different methods which are use to handle the lifecycle of fragment which are listed below :

  1. onAttach(): method called when fragment has attached to the activity
  2. onCreate(): method called after attaching the fragment to the host activity
  3. onCreateView(): method called by the fragment for drawing the user interface
  4. onViewCreated(): method called after view has been created by the onCreateView()
  5. onActivityCreated(): used to indicate that the host activity has been created
  6. onStart(): method called when the user interface is visible to the user
  7. onResume(): method called when the app user is able to interact with the user interface of the application
  8. onPause(): method called when the user is moving to the next fragment or moving back to the another fragment
  9. onStop(): method called after the onPause() method
  10. onDestroyView(): method called when the view created by the onCreateView() method has been destroyed or removed
  11. onDestroy(): method called to completely remove the fragment state and its resources from the host activity
  12. onDetach(): method called after the onDestroy() method to notify that the fragment within the host activity has been detached.

Read more about Activity lifecycle illustrated using Flow chart

Fragment Lifecycle in Android explained with Source Code

1. onAttach() 

As we have already discussed, the fragment is completely dependent on the activity and without the activity we cannot launch our fragment. This method is used to let us know that our fragment has been attached to the activity. Below is the code snippet for the onAttach() method which we can use in our application. 

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d("lifecycle","onAttach method invoked"); 
}

2. onCreate()

This method is used to create the instance of the fragment which we have to display within our activity. This method is called after onAttach() method after attaching the fragment to the host activity. Below is the code snippet for the onCreate() method which we can use in our application.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("lifecycle","onCreate method invoked");
}

3. onCreateView()

This method is called by the fragment for drawing the user interface. We can use a View component to get the UI design from our XML and return it to a fragment to provide the UI for our fragment. Below is the code snippet for the onCreateView() method which we can use in our application.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("lifecycle","onCreateView method invoked");
// below is the layout file to be inflated from layout folder
View v = inflater.inflate(R.layout.fragment_test, container, false);
return v;
}

4. onViewCreated()

This method is called when the view has been created by the onCreateView() method. This method is used when we have to display a recyclerview and configure the adapter of the recycler view before it is visible to the user. Below is the code snippet for the onViewCreated() method which we can use within our application.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d("lifecycle","onViewCreated method invoked");
}

5. onActivityCreated()

This method is used to indicate that the host activity has been created in which we are displaying our fragments. Below is the code snippet for the onActivityCreated() method which we can use within our application.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("lifecycle","onActivityCreated method invoked");
}

6. onStart()

This method is being called when the user interface is visible to the user. Below is the code snippet for the onStart() method which we can use within our application.

@Override
public void onStart() {
super.onStart();
Log.d("lifecycle","onStart method invoked");
}

7. onResume()

This method is called when the app user is able to interact with the user interface of the application. Below is the code snippet for the onResume() method which we can use within our application.

@Override
public void onResume() {
super.onResume();
Log.d("lifecycle","onResume method invoked");
}

8. onPause()

This method is called when the user is moving to the next fragment or moving back to the another fragment. This method is called when we are doing the transition or removal of the fragment from the host activity. Below is the code snippet for the onPause() method which we can use within our application.

@Override
public void onPause() {
super.onPause();
Log.d("lifecycle","onPause method invoked");
}

9. onStop()

This method is called after the onPause() method. This method is called when the fragment is removed from the host activity or the host activity has been stopped. Below is the code snippet for the onStop() method which we can use within our application.

@Override
public void onStop() {
super.onStop();
Log.d("lifecycle","onStop method invoked");
}

10. onDestroyView()

This method is called when the view created by the onCreateView() method has been destroyed. Below is the code snippet for the onDestroyView() method which we can use within our application.

@Override
public void onDestroyView() {
super.onDestroyView();
Log.d("lifecycle","onDestroyView method invoked");
}

11. onDestroy()

This method is called to completely remove the fragment state and its resources from the host activity. Below is the code snippet for the onDestroy() method which we can use within our application.

@Override
public void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy method invoked");
}

12. onDetach()

This method is being called after the onDestroy() method to notify that the fragment within the host activity has been detached. Below is the code snippet for the onDetach() method which we can use within our application.

@Override
public void onDetach() {
super.onDetach();
Log.d("lifecycle","onDetach method invoked");
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like