In android different components are used while developing applications so that a perfect end result is accomplished. Some of these components include an activity and a fragment.
Activities and fragments are quite common as they mostly work hand in hand whenever a certain feature is required.
Some other components that are used in android development include the listview, tabs, and many more.
In this article, we shall discuss the major notable differences between an activity and a fragment in android and how they work together in a project.
What is an activity in android
In android, activities are the most common elements that are found and are the most used. An activity in android is basically that component that offers the user an interface to create a screen that will have what will be visible to the users.
An activity in android is what is created when a project is created in android studio, for example, by default on every new android project that is created using android studio, an activity called Main is created by default.
The activity usually has two files, an XML file that holds the design components, for example, the buttons, edit text, and text view, and also a java (.java) or kotlin (.kt) depending on the language selected where the functionality of the XML components are implemented.
The combination of the two files, that is, XML and java or kotlin, forms an activity in android meaning without the two files, an activity cannot be complete. You can learn to create an activity in android studio by following the steps outlined here.
What is a fragment in android
A fragment in android is basically a component that is attached to an activity and displayed as part of that activity.
The good thing with a fragment is that it can be used within activities by just calling it to the specific position within an activity.
To understand a fragment better in android, think of it as a sub activity such that it cannot stand on its own, it is a child activity that requires the activity itself to run.
You can have more than one fragment in a single activity and they will work perfectly as long they do not overlap each other.
An example of an app that uses fragments is WhatsApp, in that, the 3 tabs in WhatsApp, that is, Chats, Status, and Calls are all in one activity but they are different sub activities called fragments.
Tabs in android are a good example of how fragments are used in android. To learn more about fragments in android, follow this article on how to create tabs using fragments.
Differences between activities and fragments in android
Now that we have defined both activities and fragments in android, it's time we understand the major differences that are visible between the two.
An activity in android is a stand-alone component that does not require other components to execute while a fragment requires an activity for it to be executed meaning it is dependent on an activity. Also, it means that an activity can exist without a fragment while a fragment cannot
All activities present in an android project must be defined in the android manifest file while no fragment is defined in the android manifest file since it is not an executable component.
An activity can be set as the launcher activity while a fragment cannot be set as a launcher activity even if it has the design that will be displayed on the first screen of the app.
Creating a multiple screen user interface cannot be achieved using a single activity but it can be achieved using multiple fragments in the same activity.
Both activities and fragments in android are important depending on the function of each. It is therefore necessary to understand where each component is used while developing an application.
Having defined what is an activity in android and how to create an activity in android, what is a fragment in android and also discussed the differences between activities and fragments, therefore that will be all for this article.
Follow us for more helpful content.