Android – Fragment and Fundamentals

Fragment and Fundamentals

When the screen of an Android App is so large that it is difficult to manage all its functionalities in a single activity , in that case, that big activity is divided into many small sub-activities. These small sub- activities are called Fragments and an activity can display one or more Fragments on the screen at the same time.

fragment

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 an Fragment is embedded falls on the lifecycle of the Fragment. That is, if the end of the life cycle of the parent activity is over, 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 as a Part of its Activity Layout a Fragment she Fragment our Activity Views has its own separate Fragment Layout Define stays within the Layout or ViewGroup the Hierarchy and 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 we can create our entire Android App without using Fragment. 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.