Android Fragment Lifecycle – multiple screen support

Android 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 or the multiple screen size support.
In other words there are many smartphone on the market with different screen resolution and different screen density. To make the situation more complex, if possible, there are not only smartphone but tablets also. It is clear to everyone that there are big differences between these two device classes in terms of screen size. 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. 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. What said until now is known as multiple screen support. 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. This is a technique that can be still used but it isn’t enough.
A 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. Well in this case we could have different behaviours if the app runs on a smartphone or tablet.

On a smartphone we must have two activities to accomplish this task.

and when user clicks on an item

while in the tablet case we want to “use” all the device screen to show the details near the item list.
As it is clear from the example above we would like to have a way to combine “activities” so that they can be used at the same time or in a sequence where one calls the other. We want to organize the screen layout without rewriting the code. This can’t be done only using multiple layouts we need something else

Android Fragment

Since Android 3.0 was introduced a new “concept” called Fragment. A fragment is a piece of android code with its layout that can be arranged and combined together to have different layouts. Using fragments we can re-use the code and satisfy at the same time the device screen size requirements. A fragment can’t live by itself but only inside an Activity and this one can hold several fragments. 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. So using fragment we can manipulate the master-detail app (shown above) in this way:
on a smartphone

and when it runs on a tablet we have:

Android Fragment Lifecycle

Now we know when it is useful to use fragments, we need to know how they work and the Android fragment lifecycle. 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. An Android fragment lifecycle is more complex than the activity lifecycle because it has more states. These lifecycle states are shown below:

Let’s move from the top to the bottom. At very beginning of the fragment life the method onInflate is called. We have to notice that this method is called only if we define fragment directly in our layout using the tag <fragment>. In this method we can save some configuration parameter and some attributes define in the XML layout file. 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. After it we have 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 as we do for example in the ListView widget. 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. 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.When the fragment is ready to interact with user onResume is called. At the end of this phase our fragment is up and running!!
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.

How to create a Fragment

Once we know the fragment lifecycle, we still need to know how to create it and attach it to an activity. The first thing we need to know is that if we want to create our fragment we have to extend android.app.Fragment. So let’s suppose we have a fragment called Fragment1, to create and define it we have:

public class Fragment1 extends Fragment {
...
}

As we said before, a fragment exists only inside an Activity so need to define it somehow somewhere. We have two choices:

While if we define our fragment using a place holder we need to do some other work.

Layout Frameset and Fragment

If we define our fragment inside XML layout file we don’t have much freedom to modify dynamically the fragment. There’s another way we can use to achieve more freedom: we can use <FrameLayout>. In the XML layout file we have:

About The Author

I'm an electronic engineer with over 15 years of
experience in computer programming. I'm the author of the book "Android things projects". I'm a MVB at DZone and DZone Guide Author. I have contributed to the IoT guide. I'm the technical reviewer of the book Internet of Things with Arduino Blueprints(published by PacktPub). I love creating IoT projects using Arduino, Raspberry Pi,
Android, and other platforms. I am interested in the convergence
between IoT and the mobile applications. I'm SCEA, SCWCD, and SCJP
certified.
Follow on: Twitter | Linkedin

Fragment is very hot topic. and i am sure user will get complete idea about fragment after reading this. While reading fragment, Action bar is very important topic. Seeing action bar integration with Fragment will make your life easy

Sign up for our Newsletter

Enter your email and stay on top of things,

Get SwA updates delivered to your inbox. Article, tips, source code

×

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.