HomeFragments in Android ApplicationsCodingAndroidJavaFragments in Android Applications

Fragments in Android Applications

Hello everyone. If you read my other articles in the wecodeplatform, you know that generally my articles are about android application development. I am trying to give general information about various topics on android application development in most of my articles.

This article will be about fragments. Fragments are an important part of the Android Application Development. So in this article, I want to create a general concept about fragments in your mind.

I want to start with ‘’what is fragment?’’.

We can say for a fragment, it is a small particle of activity which is created inside the activity. But fragments are different from normal activities. Fragments have their own life cycle.

Fragments are serviced with android 3.0 to developers. Fragments provide developers a flexible working environment.

For example, you can add more than one fragment inside one activity. So that you can design multiple user interfaces. I will give design examples about that below.

We use activity in most of the Android Apps. If you think about these apps that you used activity in it, these apps always have one user interface. And users had to pass to another activity when they wanted to make a different process. That is different in fragments.

You can show users more than one user interface on the same screen. And all these interfaces can run independently. Because all fragments have their own lifecycle.

Also, You can use one fragment for more than one activity. You can use one design on different activities. It provides convenience in this way. You don’t have to design that part and write its codes again. You can call the fragment in different activities and run it.

Again, thanks to fragments, you can close one fragment and open a new fragment inside an activity. So that life cycle of activity is not spoiled.

You can consider fragments as modular particles of activities by looking at all we mentioned above. These features provide an improved, flexible and easy working environment. So that they can develop better quality applications.

One more important detail about fragments, a fragment must run inside an activity. In other words, the fragment can’t run itself. And the fragment life cycle depends on the activity life cycle. If an activity closes, all depending fragments are also closed.

We can look at the picture above if we want an example usage of fragments. Android uses this example in its official site to explain fragments. Using fragments on tablets gives more advantages. Because tablets have wider screens, users can take advantage of widescreen.

In this example, there are two fragments on the tablet screen. Let’s assume that this application is made for reading different articles. On the left hand side, there can be articles in list form. When a user clicks on an article from that list, the article can be opened on the right hand side fragment.

So that the user can pick articles from the left side and can read chosen article content on the right side.

Ability to do these more than one processes in one screen, increase UX/UI quality of application. So, user experience is getting higher.

Without fragment, users have to choose an article in one activity and must open another activity to read content of the article. If the user wants to turn the article list page, the article content page which is inside one activity must be closed and the article list page which is inside another activity must be opened.

Same example design shown in the right hand side for phones. But there is not enough space to show two fragments in one phone screen. So, two different fragments located inside two different activities.

When a user selects an article from a list, new activity opens and article content is shown inside the fragment.

You can see many fragment advantages for user experience in different applications. I want to show a different usage example of fragments over another application design.

In this picture, there is an application created with android studio for messaging. That application can be similar to ‘’whatsApp’’ application. In that application, three fragments used in one activity.

First section is to see friend requests.

Second one is the messaging section.

Third fragment is a list where all friends are inside.

The important thing here is, there are three fragments inside one activity and all fragments running independently. You don’t need to close activity to pass one fragment to another fragment. Because all fragments have their own life cycle, they all run independently. And all fragments have their own user interface.

For example, in the first section you can accept or reject friend requests.

In the second section, you can continue chatting.

And in the friends section, you can see the friend list and their contact information.

A Design like that increases user experience. You can make that happen by using fragments. We can give so many examples like that. But I think these examples are enough for this article.

As you can see, Fragments provide us great convenience in Android Application Development. As you start using fragments, you will see that fragments are indispensable in increasing the user experience.

So, I hope I was able to give you useful information about fragments. See you in another article.