Difference Between Activity and Fragment in Android

Activity

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with R.attr.windowIsFloating set), Multi-Window mode or embedded into other windows. There are two methods almost all subclasses of Activity will implement:

  • onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
  • onPause() is where you deal with the user pausing active interaction with the activity. Any changes made by the user should at this point be committed (usually to the ContentProvider holding the data). In this state the activity is still visible on screen.

To be of use with Context.startActivity(), all activity classes must have a corresponding <activity> declaration in their package’s AndroidManifest.xml.

Fragment

A Fragment represents a reusable portion of your app’s UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own–they must be hosted by an activity or another fragment. The fragment’s view hierarchy becomes part of, or attaches to, the host’s view hierarchy. We can add or remove fragments in an activity while the activity is running.

Activity is the UI of an application through which user can interact and Fragment is the part of the Activity, it is a sub-Activity inside activity which has its own Life Cycle which runs parallel to the Activities Life Cycle.

Difference Table

ActivityFragment
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.
Activity is not dependent on fragment Fragment is dependent on activity. It can’t exist independently.
we need to mention all activity it in the manifest.xml file Fragment is not required to mention in  the manifest file
We can’t create multi-screen UI without using fragment in an activity,After using multiple fragments in a single activity, we can create a multi-screen UI.
Activity can exist without a Fragment  Fragment cannot be used without an Activity.
Creating a project using only Activity then it’s difficult to manageWhile Using fragments in the project, the project structure will be good and we can handle it easily.
Lifecycle methods are hosted by OS. The activity has its own life cycle.Lifecycle methods in fragments are hosted by hosting the activity.
Activity is not lite weight. The fragment is the lite weight.

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!


Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy, KFC


One comment

Leave a Reply

Your email address will not be published.