Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between activity and fragment?

As per my research, there is a significant difference in the concept of backstack and how they exist:

Activity

  • 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.

Fragment

  • 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?

like image 226
Devrath Avatar asked Sep 13 '14 11:09

Devrath


People also ask

What is difference between activity and fragment?

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.

What are fragments and activities?

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.

Should I use an activity or fragment?

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.

Why fragments are better than activities?

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).


2 Answers

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

like image 77
Юрій Мазуревич Avatar answered Oct 21 '22 12:10

Юрій Мазуревич


Main Differences between Activity and Fragment

  1. Activity is an application component that gives a user interface where the user can interact. The fragment is a part of an activity, which contributes its own UI to that activity.
  2. For Tablet or if mobile is in landscape then Using fragment we can show two lists like the only list for show the state name and other lists will show the state description in single activity but using Activity we can't do the same thing.
  3. Activity is not dependent on fragment.but The fragment is dependent on Activity, it can't exist independently.
  4. without using fragment in Activity we can't create multi-pane UI. but using multiple fragments in a single activity we can create multi-pane UI.
  5. If we create a project using only Activity then its difficult to manage but if we use fragments then the project structure will be good and we can handle it easily.
  6. An activity may contain 0 or multiple numbers of fragments. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.
  7. Lifecycle methods are hosted by OS. The activity has its own life cycle. Lifecycle methods in fragments are hosted by hosting the activity.
  8. For Activity, we just need to mention in Manifest but for fragment its not required.
  9. Activity is not lite weight. The fragment is the lite weight.
like image 35
siddhartha shankar Avatar answered Oct 21 '22 14:10

siddhartha shankar