Activity states | Fragment callbacks | |
Activity created | onAttach() onCreate() onCreateView() onActivityCreated() | onAttach(Context) This happens when the fragment is associated with a context onCreate(Bundle) This is very similar to the activity’s onCreate() method. It can be used to do the initial setup of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) Fragments use a Layoutinflater to create their views at this stage onActivityCreated(Bundle) Called when the onCreate() method of the activity has completed. |
Activity started | onStart() | onStart() Called when the fragment is about to become visible. |
Activity resumed | onResume() | onResume() Called when the fragment is visible and actively running. |
Activity paused | onPause() | onPause() Called when the fragment is no longer interacting with the user. |
Activity stopped | onStop() | onStop() Called when the fragment is no longer visible to the user. |
Activity destroyed | onDestroyView() onDestroy() onDetach() | onDestroyView() Gives the fragment the chance to clear away any resources that were associated with its view. onDestroy() In this method, the fragment can clear away any other resources it created. onDetach() Called when the fragment finally loses contact with the activity. |
Fragments inherit lifecycle methods
Even though fragments have a lot in common with activites, the Fragment class doesn’t extend the Activity class. This means that some methods that are available to avtivites aren’t available to fragments. Note that Fragment class doesn’t implement the Context class. Unlike an activity, a fragment isn’t a type of conext and therefore doesn’t have direct access to globalinformation about the application environment.Instead, fragments must access this information using the context of other objects such as its parent activity.