Sunday , January 16 2022

What is Fragment in Android – Fragment Lifecycle

What is Fragment in Android – Fragment Lifecycle – When the screen of an Android App is so large that it is difficult.

What is Fragment in Android – Fragment Lifecycle

These small sub-activities are called Fragments and an activity can display one or more Fragments on the screen at the same time.

When the screen is small, an activity usually has only one Fragment and the same Fragment appears equally on the big screen.

The biggest feature of fragments is that once we have created, we can reuse them again and again in as many activities as we want. Also, like Fragment, each Fragment has its own separate lifecycle and each Fragment itself can independently receive and respond to Separate Input Events, which have no direct connection to the Main Activity.

A Fragment is always required to be embedded in some activity and the Direct Effect of the lifecycle of the activity in which a Fragment is Embedded falls on the Lifecycle of the Fragment. That is, if the life cycle of the parent activity ends, then the life cycle of the fragrance, even if it is being run, also ends. Similarly, when Parent Activity Pause occurs, all its Embedded Fragments also become Pause.

However, when the activity is in the Resumed or Running Stat, then in that case we can freely access and manipulate all its fragments. That is, we can freely add / remove one or more fragments in any of our activities according to our needs.

When we add a Fragment as a part of our Activity Layout, that Fragment remains within the View Hierarchy Layout ie ViewGroup of our Activity and defines its own separate Fragment Layout there.

To insert a Fragment into our Activity Layout, we have to use the Element within the Existing ViewGroup in our Activity Layout File or it can also be added through the Code in the Code File of our application. , About which we shall learn further.

Although we can add a Fragment to our activity as per our need and desire, it is not necessary to use it compulsory in any Android App and without using Fragment we can create our entire Android App.

Also, when we use a fragment, it is not necessary that the UI part of our fragment is also created. If we wish, we can also create Fragment as an Invisible Background Worker without UI, which invisibly Fulfill any Functionality required for our Main Activity.

Fragments Life Cycle

  • onCreate () – Its method is used to initialize the fragment.
  • onAttach () – This method is called when a fragment is added to the activity for the first time.
  • onActivityCreated () – This method is implemented after completion of the onCreate () method.
  • onCreateView () – This method is called by the android system when fragment UI shows for the first time
  • onStop () – This method is called when fragment stop.
  • onResume () – This method is called when fragment makes interactive.

Read Also – What is Activity in Android Studio – The Basic Fundamentals

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

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