Activity vs Fragment Lifecycle [closed]

 Differences between Activity and Fragment lifecyle in Android

Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity. Fragments are used to efficiently use the space in wider screen devices.

An activity may contain 0 or multiple number of fragments based on the screen size. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.

A fragment can't exist independently. It should be always part of an activity. Where as activity can exist with out any fragment in it.

A fragment lifecycle is more complex than the activity lifecycle because it has more states. Lifecycle states are shown below:

enter image description here

onInflate

At very beginning of the fragment life the method onInflate is called. In this method we can save some configuration parameter and some attributes define in the XML layout file.

onAttach

After this step onAttach is called. This method is called as soon as the fragment is “attached” to the “father” activity and we can this method to store the reference about the activity.

onCreate

It is one of the most important step, our fragment is in the creation process. This method can be used to start some thread to retrieve data information, maybe from a remote server. The onCreateView is the method called when the fragment has to create its view hierarchy.During this method we will inflate our layout inside the fragment.

During this phase we can’t be sure that our activity is still created so we can’t count on it for some operation. We get notified when the “father” activity is created and ready in the onActivityCreated.

From now on, our activity is active and created and we can use it when we need.

onStart

The next step is onStart method. Here we do the common things as in the activity onStart, during this phase our fragment is visible but it isn’t still interacting with the user.

onResume

When the fragment is ready to interact with user onResume is called.

Then it can happen that the activity is paused and so the activity’s onPause is called. Well onPause fragment method is called too.

After it, it can happen that the OS decides to destroy our fragment view and so onDestroyView is called. After it, if the system decides to dismiss our fragment it calls onDestroy method.

Here we should release all the connection active and so on because our fragment is close to die. Even if it is during the destroy phase it is still attached to the father activity. The last step is detach the fragment from the activity and it happens when onDetach is called.

I hope you can understand from this.

Thanks.


Directly from developer Fragments guide -

The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment, such that each lifecycle callback for the activity results in a similar callback for each fragment.

For example, when the activity receives onPause(), each fragment in the activity receives onPause().

Fragments have a few extra lifecycle callbacks, however, that handle unique interaction with the activity in order to perform actions such as build and destroy the fragment's UI. These additional callback methods are:

1) onAttach() =>

Called when the fragment has been associated with the activity (the Activity is passed in here).

2) onCreateView() =>

Called to create the view hierarchy associated with the fragment.

3) onActivityCreated() =>

Called when the activity's onCreate() method has returned.

4) onDestroyView() =>

Called when the view hierarchy associated with the fragment is being removed.

5) onDetach() =>

Called when the fragment is being disassociated from the activity.

Once the activity reaches the resumed state, you can freely add and remove fragments to the activity. Thus, only while the activity is in the resumed state can the lifecycle of a fragment change independently.

However, when the activity leaves the resumed state, the fragment again is pushed through its lifecycle by the activity.

Check Activity and Fragments.