Fragment Lifecycle - KingGainer07/AndroidGuide GitHub Wiki
Fragment Lifecycle In Android:
Table Of Content
- Fragment Lifecycle In Android:
- Fragment Lifecycle Example In Android Studio:
- Output:
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 hierarchy, state and attachment to its activity.
Here is the list of methods which you can to override in your Fragment class −
- 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.
Below is the example code of onAttach() method.
@Override public void onAttach(Activity activity) { super.onAttach(activity); // add your code here which executes when fragment instance is associated }
- 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.
Below is the example code of onCreate() method.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// add your code here which executes when fragment's instance initializes
}
- 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.
Below is the example code of onCreateView() method.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_test, container, false);
// add your code here to draw the UI for the first time means in this method we can get the reference of the views which are created // in our xml file
return v;
}
- 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.
Below is the example code of onViewCreated() method.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// add your code here which executes after the execution of onCreateView() method.
}
- 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.
Below is the example code of onActivityCreated() method.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// add your code here which executes when the host activity is created.
}
- onStart(): This method is called once the fragment gets visible.
Below is the example code of onStart() method.
@Override
public void onStart() {
super.onStart();
// add your code here which executes when the Fragment gets visible.
}
- onResume(): This method is called when the Fragment is visible and intractable.
Below is the example code of onResume() method.
@Override
public void onResume() {
super.onResume();
// add your code here which executes 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.
Below is the example code of onPause() method.
@Override
public void onPause() {
super.onPause();
// add your code here which executes when user leaving the current fragment or fragment is no longer intractable.
}
- 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.
Below is the example code of onStop() method.
@Override
public void onStop() {
super.onStop();
// add your code here which executes Fragment going to be stopped.
}
- 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.
Below is the example code of onDestroyView() method.
@Override
public void onDestroyView() {
super.onDestroyView();
// add your code here which executes when the view's and other related resources created in onCreateView() method are removed
}
- onDestroy(): This method is called to do final clean up of the Fragment’s state but Not guaranteed to be called by the Android platform. This method called after onDestroyView() method.
Below is the example code of onDestroy() method.
@Override
public void onDestroy() {
super.onDestroy();
// add your code here which executes when the final clean up for the Fragment's state is needed.
}
- 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.
Below is the example code of onDetach() method.
@Override
public void onDetach() {
super.onDetach();
// add your code here which executes when fragment has been disassociated from its hosting activity
}
Fragment Example In Android Studio:
Step 1: Create a new project and name it AndroidFragmentDemo
Step 2: Open res -> layout ->activity_main.xml (or) main.xml and add following code:
In this step we open xml file and then create a fragment to display the Fragment in our Activity.
Step 3: Now we need a Fragment and a xml layout(xml) file. So create a new Fragment by right click on your package folder and create class and name it FragmentOne,FragmentTwo and add the following code in it.
Step 4: Open src -> package -> MainActivity.java.
The following code in it.
Step 5: Run and Output The following code in it.