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

After it it can happen that the OS decides to destroy our fragment view and so onDestroyView is called. Well onPause fragment method is called too. Then it can happen that the activity is paused and so the activity’s onPause is called. At the end of this phase our fragment is up and running!! 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.When the fragment is ready to interact with user onResume is called. From now on, our activity is active and created and we can use it when we need. We get notified when the “father” activity is created and ready in the onActivit圜reated. During this phase we can’t be sure that our activity is still created so we can’t count on it for some operation.

During this method we will inflate our layout inside the fragment as we do for example in the ListView widget. The onCreateView is the method called when the fragment has to create its view hierarchy. This method can be used to start some thread to retrieve data information, maybe from a remote server. It is one of the most important step, our fragment is in the creation process. 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. In this method we can save some configuration parameter and some attributes define in the XML layout file. We have to notice that this method is called only if we define fragment directly in our layout using the tag. At very beginning of the fragment life the method onInflate is called.
Fragment lifecycle android#
An Android fragment lifecycle is more complex than the activity lifecycle because it has more states. A fragment lives only inside an Activity that acts as a container.Each fragment has its own view hierarchy that can be inflated as we do usually. Now we know when it is useful to use fragments, we need to know how they work and the Android fragment lifecycle. So using fragment we can manipulate the master-detail app (shown above) in this way: It is important to note that fragments can be combined with other activity elements so that you don’t have to rewrite all the activity interface. A fragment can’t live by itself but only inside an Activity and this one can hold several fragments.

Fragment lifecycle code#
Using fragments we can re-use the code and satisfy at the same time the device screen size requirements. A fragment is a piece of android code with its layout that can be arranged and combined together to have different layouts. Since Android 3.0 was introduced a new “concept” called Fragment. Well in this case we could have different behaviours if the app runs on a smartphone or tablet. This is a technique that can be still used but it isn’t enough.Ī classic example it is an application that have an item list and when user clicks an item in this list the app shows the item details.
Fragment lifecycle how to#
In a previous post ( How to support multiple screen in Android), i talked about how to support multiple screen using Android features like creating different layouts and so on. What said until now is known as multiple screen support.

This creates many problems because we have to adjust the application so that it behaves differently when it runs on a smartphone or a tablet. Well, when we develop an application we have to keep in mind that it can works on different devices and it has to be optimised so that the user experience is fully satisfied. It is clear to everyone that there are big differences between these two device classes in terms of screen size. To make the situation more complex, if possible, there are not only smartphone but tablets also. In other words there are many smartphone on the market with different screen resolution and different screen density. Android Fragment lifecycle is an important aspect to take into account before using fragments.Įvery developer, that wants to develop an application in Android, has to face the mobile phone fragmentation problem or the multiple screen size support.
