ABSTRACT

In Chapter 4, we created a Quiz app where one Activity displays ten questions at a time by reusing the views. There is a disadvantage to constantly redrawing the widgets at the beginning of each question in the Activity, although you might not notice the lagging in performance because of how minor and little the application data is but when dealing with a major application and massive data, this can affect the speed of application processing.

An Android component called Fragment defines an independent user interface without starting an entirely new Activity. A fragment has its lifecycle but cannot exist on its own; it can only run within an Activity. Using Fragments enables you to create flexible user interfaces for Android devices of different screen sizes, especially multipane tablets.

Throughout this chapter, you will learn how to use fragments with ViewPager2 by building an Image slider application. You will be able to swipe through different images on a single screen with the help of the fragment component and understand how they attach themselves to an Activity.