Android activity and fragment

Android activity and fragment
December 2, 2019 No Comments Android Development,Development Pushpendra Kumar

Hi Guys, Welcome here! If you are looking for the deep difference in between android activity and fragment. Then you on the correct page. So without waisting time let’s see the detailed difference both android activity and android fragment.

Difference in Android activity and fragment

An Activity is an application component that provides a screen, with which users can interact in order to do something.

Whereas a Fragment represents a behavior or a portion of user interface in an Activity.

A Fragment is a section of an Activity, which has:

  • Its own lifecycle
  • Receives its own input events
  • We can be added or removed fragment while Activity is running.

Activities are one of the fundamental building blocks of apps on the Android platform. They serve as the entry point for a user’s interaction with an app and are also central to how a user navigates within an app (as with the Back button) or between apps (as with the Recents button).

Skillfully managing activities allows you to ensure that, for example:

  • Orientation changes take place smoothly without disrupting the user experience.
  • Its own lifecycle, This worked with fragment lifecycle.
  • When we open another activity from existing activity then existing activity will remain same with the data. In conclusion activity does not lost it’s data while transition come into picture.

Some common difference in Android activity and fragment

  • Activity is an application component which give user interface where user can interect. Fragment is a part of an activity,which contibute its own UI to that activity.
  • without using fragment in Activity we can’t create multi-pane UI.but using multiple fragments in single activity we can create multi-pane UI.
  • An activity may contain 0 or multiple number of fragments. A fragment can be reused in multiple activities, so its act like a reusable component in activities.
  • For Activity we must need to mention in Manifest but for fragment its not required.

Example of Android activity and fragment

  • Create new android project as I have already described in last tutorial. And also in last video.
  • After that create copy and paste the existing code into your MainActivity.java file

After writing the above code then just start your android application. Once you start your application then your activity will execute the function as given into the below image.

Activity lifecycle
Basic Life cycle of an activity

After that we will create an fragment to see the lifecycle of android fragment. So let’s begin with it.

  • Create new fragment inside your project. As I have already described in given video.
  • After that, First edit your activity_main.xml, as I did.

That’s cool, Now replace your onCreate() method in MainActivity.java

Create now after that, Create a hierarchy of lifecycle of Android fragment into the your_fragment_name.java

That’s great, Now your fragment will behave as per given image. And the hierarchy also will work in same manner.

fragment lifecycle
Complete lifecycle of android fragment

Conclusion of Difference in Android activity and fragment

In Conclusion, I hope it’s very clear to you that what is the difference in android activity and android fragment. And I am sure that you also understand the lifecycle of an activity with the help of given example, But If still did not understand then please go through with the video. So guys once again thank you so much fo visiting on this tutorial have a great day and great time 😎😎😎

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *