Fragment lifecycle in android

Fragment Lifecycle:

Since fragments are designed to be modules that are swapped in and out as required, they will not continuously follow the standard lifecycle of associate application. Instead, fragments have their own lifecycle events for making, starting, stopping, and destroying themselves that is loosely tied to containing activity’s lifecycle.

onAttach(Activity)
This technique is termed once once the fragment has been additional to associate activity mistreatment the fragment manager. The activity passed into this technique is that the activity that the fragment has been additional to. This technique is usually wont to verify if the containing activity is appropriate to host the fragment.

onCreate(Bundle)
Much like with activities, this technique is termed to try to to the initial creation of the fragment. this is often wherever you’d setup any member variables and begin loading any information that is required for the fragment. in contrast to with activities, you can’t access any views from this technique as a result of the UI for the fragment has not been created however.

onCreateView(LayoutInflator, ViewGroup, Bundle)
This technique is termed once it is time to form the read for the fragment. the only purpose of this technique is to form all layouts and views, seemingly by inflating a layout file, that are utilized in the fragment and come them in order that they will be additional to the activity UI structure. This technique is termed once onCreate() the primary time it executes, however will execute during a totally different order counting on however the fragment is shown. If the fragment is resuming from a suspended state, this technique are going to be known as to recreate the fragment’s read from the suspended state. a lot of thereon within the lifecycle events below.

onActivityCreated(Bundle)
This technique is termed once the containing activity has finished corporal punishment its own onCreate() technique if the fragment was additional to the activity throughout onCreate(). If the fragment is additional to the activity once onCreate() has already finished, this technique can still be known as straight off once onCreateView() is termed. This technique are often wont to alter views and connect information to your views mistreatment the information that was setup in onCreate() and also the views setup in onCreateView(). it is important that you just do not try this setup throughout onCreateView() as any operations performed therein technique will cause your read creation in alternative categories to require longer than necessary.

onStart()
This is wherever lifecycle ways begin to tie during a bit nearer to the activity lifecycle. The onStart() technique is termed once the fragment is initial created visible to the user, once onStart() is termed within the containing activity. If onStart() has already dead within the activity, this technique continues to be known as once onActivityCreated() to take care of lifecycle event consistency.

onResume()
This technique will virtually an equivalent actual factor because the activity’s onResume() technique. At now, the fragment is within the foreground and acceptive user interactions. like onStart(), this technique is termed once the activity calls onResume() and, if onResume() has already been known as within the activity, then this technique can continuously follow onStart() to take care of lifecycle event consistency.

onPause()
As with associate activity, the onPause() technique is termed once the fragment will still be seen within the app, however isn’t any longer acceptive user interaction. this might be as a result of the containing activity has been paused or as a result of a fraction operation is modifying the activity and contained fragments. The onPause() technique must always be the primary technique known as once a fraction is being hidden or destroyed moreover.

onStop()
Again, like the activity, the onStop() technique is termed once the fragment isn’t any longer visible to the user. this is often either as a result of the containing activity has been stopped or the fragment is being hidden or removed via a fraction dealings. this will conjointly occur if the fragment is pushed onto the back stack.

onDestroyView()
This technique is termed once onStop() and is employed to scrub up any lingering resources related to a fragment’s layout. once fragments aren’t any longer visible to the user, the system saves their state in order that they will be quickly remodeled, however the views are destroyed so as to save lots of memory. once this technique is termed, you’ll not access a fragment’s layout and associated views. If the fragment is resumed, onCreateView() is termed once more followed by all alternative lifecycle events necessary to bring the fragment back to the foreground. If the fragment is instead faraway from the activity fully, or the activity itself finishes, then the fragment can continue through the below lifecycle events to be destroyed.

onDestroy()
This technique is termed once the fragment isn’t any longer required and is getting ready to be destroyed by the system. At now you must finish off any remaining fragment information and unharness any connections to system resources entirely.

onDetach()
This is the last technique within the fragment lifecycle associated is termed to signal that the fragment isn’t any longer related to an activity. This technique is incredibly seldom used and you seemingly will not ever got to alter it in your applications.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories