Skip to main content

Fragment LifeCycle

Normal setRetainInstance(true) Description
onAttach onAttach Called once the fragment is associated with its activity.
onCreate - Called to do initial creation of the fragment.
onCreateView onCreateView Creates and returns the view hierarchy associated with the fragment.
onActivityCreated onActivityCreated Tells the fragment that its activity has completed its own Activity.onCreate().
onViewStateRestored onViewStateRestored Tells the fragment that all of the saved state of its view hierarchy has been restored.
onStart onStart Makes the fragment visible to the user (based on its containing activity being started).
onResume onResume Makes the fragment begin interacting with the user (based on its containing activity being resumed). As a fragment is no longer being used, it goes through a reverse series of callbacks:
onPause onPause Fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
onSaveInstance onSaveInstance Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance of its process is restarted.
onStop onStop Fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
onDestroyView onDestroyView Allows the fragment to clean up resources associated with its View.
onDestroy - Called to do final cleanup of the fragment's state.
onDetach onDetach Called immediately prior to the fragment no longer being associated with its activity.


Android's default behaviour when configuration changes
One of configuration changes to be able to test is device rotation. Android destroy and create again activity and it will affect the lifecycle of the activity. Android will restore layout when it recreate activity and fragment.

setRetainInstance
Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated: onCreate and onDestroy will not be called. Default value is false. One of practices for this option is to use Asynchronous operation.

Comments