As per my research, there is a significant difference in the concept of backstack and how they exist:
When an activity
is placed to the backstack
of activities
the user can navigate back to the previous activity by just pressing the back
button.
Activity
can exist independently.
When an fragment
is placed to the activity we have to request the instance to be saved by calling addToBackstack()
during the fragment
transaction
.
Fragment
has to live inside the activity
Are there any additional differences?
Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Fragment is dependent on activity. It can't exist independently.
Activity is the part where the user will interacts with your application. In other words, it is responsible for creating a window to hold your UI components. (UI components and how to build a layout will be discussed in another article). Fragment represents a behavior or a portion of user interface in an Activity.
Fragments are the reusable , Modular UI Component While Activity is the building block of User Interface. you can create your UI using Activity. Fragments does not have it's own controller. while Activity has.
Like activities, they have a specific lifecycle, unlike activities, they are not top-level application components. Advantages of fragments include code reuse and modularity (e.g., using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets).
Those are two completely different things:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: https://developer.android.com/guide/components/activities/intro-activities
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. https://developer.android.com/guide/fragments
Main Differences between Activity and Fragment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With