What is Fragment lifecycle in Android SDK?

In Android development, a Fragment represents a portion of the user interface in an Activity. Understanding the Fragment lifecycle is crucial for managing the UI's behavior efficiently, as it helps developers know how to handle user interactions, data loading, and resource management. The Fragment lifecycle is closely tied to its host Activity, and it includes a series of states and callbacks that dictate how the Fragment behaves over time.

Fragment Lifecycle States

The Fragment lifecycle consists of the following key methods:

  • onAttach(): Called when the Fragment is first attached to its context.
  • onCreate(): Initializes the Fragment; used for non-UI related tasks.
  • onCreateView(): Used to inflate the Fragment's layout.
  • onActivityCreated(): Called when the host Activity's onCreate() method has returned.
  • onStart(): Makes the Fragment visible to the user.
  • onResume(): The Fragment becomes active and resumes interaction.
  • onPause(): Called when the Fragment is no longer interacting with the user.
  • onStop(): Makes the Fragment invisible to the user.
  • onDestroyView(): Cleans up resources associated with the Fragment's UI.
  • onDestroy(): Final cleanup of the Fragment before it is destroyed.
  • onDetach(): Called when the Fragment is detached from its Activity.

Example: Fragment Lifecycle


    public class MyFragment extends Fragment {
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            // Perform actions when the fragment is attached
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Initialize fragment properties
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                                 Bundle savedInstanceState) {
            // Inflate the fragment layout
            return inflater.inflate(R.layout.fragment_my, container, false);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            // Activity's onCreate() execution completed
        }

        @Override
        public void onStart() {
            super.onStart();
            // Called when the Fragment is visible to the user
        }

        @Override
        public void onResume() {
            super.onResume();
            // Resume interaction
        }

        @Override
        public void onPause() {
            super.onPause();
            // Pause the interaction
        }

        @Override
        public void onStop() {
            super.onStop();
            // Fragment is not visible
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            // Clean up UI resources
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            // Clean up any remaining resources
        }

        @Override
        public void onDetach() {
            super.onDetach();
            // Fragment is detached from the Activity
        }
    }
    

Fragment lifecycle Android Fragment lifecycle Fragment lifecycle states Android development