Fragment Lifecycle In Android:

Fragment Lifecycle In Android

A Fragment represents a behavior or a portion of user interface in a FragmentActivity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a “sub-activity” that you can reuse in different activities).

Fragment Lifecycle In Android

In Android, Fragments have their own life cycle very similar to an Activity but it has extra events that are particular to the Fragment’s view, state, and attachment to its activity.

 

onAttach():

The fragment instance is associated with an activity instance. This method is called first, even before onCreate() method. This method let us know that our Fragment has been attached to an activity.

onCreate():

This will be called when creating the fragment. It means when a new fragment instance initializes, which always happens after it attaches to the host.

onCreateView():

The will be called when it’s time for the fragment to draw its UI(user interface) for the first time. To draw a UI for our fragment we must return a View component from this method that is the root of our fragment’s layout. We can also return null if the fragment does not provide a UI.

onCreateView():

The will be called when it’s time for the fragment to draw its UI(user interface) for the first time. To draw a UI for our fragment we must return a View component from this method that is the root of our fragment’s layout. We can also return null if the fragment does not provide a UI.

onViewCreated():

This will be called after onCreateView() method. This method is particularly useful when inheriting the onCreateView() method implementation but we need to configure the resulting views such as with a ListFragment and when to set up an adapter.

onActivityCreated():

This method is called after the onCreateView() method when the host activity is created. This method indicates that the activity’s onCreate() has completed.

onStart():

This method is called once the fragment gets visible.

onResume():

This method is called when the Fragment is visible and intractable.

onPause():

This method is the first indication that the user is leaving the current fragment or fragment is no longer interactable. It occurs when any Fragment Transition processed or Fragment is removed.

onStop():

This method is called after onPause() method. Fragment going to be stopped by calling onStop(). This method calls when the Fragment is no longer visible. it occurs either after the fragment is about to be removed or Fragment Transition is processed(replace Fragment) or when the host activity stops.

onDestroyView():

This method is called when the view and other related resources created in onCreateView() method is removed from the activity’s view hierarchy and destroyed.

onDestroy():

This method is called to do a final clean up of the Fragment’s state but Not guaranteed to be called by the Android platform. This method called after onDestroyView() method.

onDetach():

This method called after onDestroy() method to notify that the fragment has been disassociated from its hosting activity means Fragment is detached from its host Activity.

 

 

Facebooktwitterredditpinterestlinkedinmail
Recommended
Android Capture image from a camera and Pick Image from the…