Android Fragment Life Cycle

Fragment

Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an activity. Fragments represent multiple screen inside one activity. As we already discussed in our previous article about Activity Life Cycle.

Fragment lifecycle is an important aspect to take into account before using fragments. Every developer, that wants to develop an application in Android, has to face the mobile phone fragmentation problem( multiple screen size support)

Because an android fragment is not a view, adding it to an activity looks somewhat different than adding a view (e.g. TextView). A fragment is added to a ViewGroup inside the activity. The fragment’s view is displayed inside this ViewGroup.

Fragment is alive

First of all,the activity obtains a reference to the fragment. Then it gets a reference to the ViewGroup the fragment’s view will be rendered inside. Then the activity adds the fragment. The fragment then creates its view and returns it to the activity. The view is then inserted into the ViewGroup parent, and the fragment is alive.

Fragment Lifecycle

Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity. Each fragment has its own life cycle methods that is affected by activity life cycle because fragments are embedded in activity.The FragmentManager class is responsible to make interaction between fragment objects.

Trending TopicSkills and Responsibilities you need to be an Android Devleoper

Android fragments have their own life cycle very similar to an android activity. There are 12 lifecycle methods for fragment.

 

Android Fragment Life cycle

Below are the methods of fragment lifecycle.

onAttach() – The onAttach() method is called as the fragment is “attached” to the “father” activity and we can this method to store the reference about the activity it is called only once when it is attached with activity.

public void onAttach(Activity activity) {

super.onAttach(activity);

// add your code here which executes when fragment instance is associated

}

onCreate() – This method can be used to start some thread to retrieve data information, maybe from a remote server and It is used to initialize the fragment.

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// add your code here which executes when fragment’s instance initializes

}

onCreateView –The onCreateView is the method called when the fragment has to create its view hierarchy. You can return null if the fragment does not provide a UI.

public void onCreateView(ViewGroup container, Bundle savedInstanceState)

{

View v = inflater.inflate(R.layout.fragment_test, container, false);

return v;

}

onActivityCreated() – It is invoked after the completion of onCreate() method. view can be accessed with the findViewById() method.In this method you can instantiate objects which require a Context object

@Override

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

// add your code here which executes when the host activity is created.

}

onStart() – The onStart() method is called once the fragment gets visible.

@Override

public void onStart() {

super.onStart();

// add your code here which executes when the Fragment gets visible.

}

onResume() – The onResume method called When the fragment is ready to interact with user .

@Override

public void onResume() {

super.onResume();

// add your code here which executes when the Fragment is visible and intractable.

}

onPause() – Then it can happen that the activity is paused and so the activity’s onPause is called

@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() – The onStop() method is called when fragment is no longer visible.

@Override

public void onStop() {

super.onStop();

// add your code here which executes Fragment going to be stopped.

}

onDestroyView() – Fragment view will destroy after call this 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 called to do final clean up of the fragment’s state

@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() : The onDetach method called after onDestroy() method , to notify that the fragment has been disassociated from its hosting activity

@Override

public void onDetach() {

super.onDetach();

// add your code here which executes when fragment has been disassociated from its hosting activity

}

That’s it,Hope you guy’s got the idea of  Android fragment lifecycle Concepts.