This post is all about Android Fragments vs Activities. Let us go through a brief description of each before we see the difference. After the brief introduction we will jump directly into fragments vs activities.
Android Activity
Activity is one of the main components of an Android. The user mostly interacts with Activity in one way or another.
It holds all the logic for any screen that is being displayed. It also handles the gestures and touches to some extent. So, in some sense Activity is just a screen with extra stuff.
As we know that mobile applications are just series of screens where user touch and interact. There is a much simpler way to understand Activity.
In simple unofficial term we can understand Activity as the Screen UI. And an application can composed of one or more activity. For example: login screen, registration screen, dashboard screen, etc.
The activity takes care of all the UI related stuffs and exposes its lifecycle to developer. Multiple APIs are provided to the developer to interact with the Activity.
An Activity cannot contain other Activities. So it is not possible to create flexible layout with only Activities.
But an Activity can host multiple fragments at any given time. And a fragment cannot exist without an Activity. Which makes it one of the most essential components in Android.
Android Fragment
Fragment in android are more or less like activity itself. But the fragments are much more flexible and different than Activity.
A fragment has its own UI, lifecycle and can handle multiple events themselves too. Which makes it a viable alternative to Android activities for creating flexible layouts.
The fragments cannot not exist on their own. They should be attached to an activity. However one activity can contain multiple fragments.
And any give app already is using multiple fragments. Because it makes the code much more readable and concerns are separated.
This is specifically where the term flexible comes from. Which help us make responsive UI, imagine responsiveness just like in the web.
The description here is short and concise but there are much to explore. I suggest you do some reading on google.
Android Fragments vs Activities
Let us see the contrasting difference between an Activity and a Fragment.
Activity | Fragment |
---|---|
Activity is a single and isolated component that doesn’t depend on anything else. | Fragments cannot exist by themselves. Therefore depending on Activity. |
Activities are directly dependent on Android OS. | Fragments depend on Activity lifecycle as they run in parallel to Activity’s lifecycle. |
An activity needs to be registered in the AndroidManifest.xml | Fragments does not need to be registered anywhere as they are dynamic. |
Other applications might directly launch the allowed activity / launcher activity. | Fragments cannot be directly navigated. As the definition of fragments aren’t exposed publicly. |
Data passing between activities require serialization / marshaling. | Data passing between fragments might be facilitated through hosting activity. |
Activities are not flexible for responsive multi layout screens. | Fragments are built to provide flexibility to build multi layout screen. |
Activity creation and destruction has more overhead than that of Fragment. | Fragment creation isn’t as expensive as Activity. |
Those are the differences between Android Fragments and Android Activities.
Suggested
Retrofit File Upload using FileProvider ( for Content URIs )