I have recently been studying Android UI. In Android UI, I have been discovering ‘Fragments.’ I do not claim to know everything about Fragments but I do believe I understand the basics of them. My hope is simply to help others and maybe get some feedback on things that may be better or that might straighten out some of my ideas. Feel free to leave comments. I will probably be splitting this subject into two blog posts; one for the basic structure and idea of Activities and Fragments and another that I will like to at the bottom about how I had to implement Fragments in our app to get it to work. ENJOY!
First, let me review the connection between the different files within an Android project. We have XML files and then we have the JAVA files. There are many more but to start, a java file ‘inflates’ an XML file. This means that you create the XML file to portray the GUI the way you want your app to look. In the java code, you then need to ‘inflate’ (which seems to actually be a very appropriate word to use) the XML file to push that view to the user. This means that for each different view, you have two files; the class file and the XML layout file. This image demonstrates this kind of relationship:
While this may not seem like a lot of complexity, it becomes much more difficult when you are programming these things and dealing with scope. Here we see that there is one activity to many Fragments. The way in which I have found myself implementing fragments is by including the logic and methods within the Fragment in which the buttons belong. In other words, the Fragments contain the logic.
This is how the relationship is for using only activities:
While each of the above activities would also have a XML layout with them, the complexity comes as we discuss scope later on. The next thing to understand is how a Fragment differs from an Activity and how they work. An Activity is a view in which is relayed to the android device through an ‘intent.’ These activities can stand alone and will have the methods (such as OnClickListeners) within the class itself and will handle any logic for itself before sending the user to the next Activity.
So you may be asking yourself what the differences are between Fragments and Activities. The answer to this question is found in the implementation of these two. A Fragment is a View that cannot stand alone. It offers methods that allow you to retrieve information from the controlling Activity. The Fragments also allow you to control what happens when another Fragment is called into view. Activities, on the other hand, do not give you that same control. Activities offer more of a risk of memory leaks because of this lack of control. Usually memory leaks occur when you have some kind of object that references another. The garbage collector will not remove such objects as they may be needed and therefore you fill or use up all your memory. When you choose to use an ‘intent’ to bring up a new activity, there is little control as to what happens to that Activity when the other is called. The more Activities there are, the more opportunities there are for these memory leaks. Here are a few good posts or articles on memory leaks with android that may be helpful: google, leaky dialog, and chaos. I have yet to fully understand why Fragments don’t offer the same level of hazard but this is what I have been informed and understand.
There are more benefits to Fragments that I understand a little better. One of the great reasons that Android implemented them is to allow applications to be more dynamic with the many different screen sizes that are becoming ever more diverse. To explain further, lets think about activities first. Activities stand as a view. Without Fragments, these views are fairly static in nature and can only become larger or smaller according to the device real estate. With Fragments, one activity is or can be the parent to multiple. This means that you can even define what you want to happen with these two fragments when there is more real estate. This image may help you to visualize this better and can be found at the developer page for android:
The trouble that I had with using fragments is scope. I thought that this would be the least of my worries but when you think about it, it is easy to see why this could become so complex (at least for me). With Activities, you have the logic taking place on one View and then you simply push to another view. With Fragments, you have to pass the information from Fragment to Fragment through the Activity. This makes things much more difficult with scope. Eventually I hope to post another blog on how I had to go about implementing Fragments to get them to work and I’ll post a link to that post HERE when its done! Hopefully this was good to give some ideas as to what Fragments are vs. Activities. For your reference, here is another post that I found kind of interesting about Fragments.
Also, there are other great tutorials that can give you a kind of jump-start. My personal favorite it ‘TheNewBoston’ on YouTube.