A Fragment is an independent component that can be connected to an Activity or simply is a subactivity. Typically it defines a part of UI but can also exist with no user interface, that is, headless. An instance of fragment must exist within an activity.
Fragments ease the reuse of components for different layouts. Fragments are the way to support UI variances across different types of screens. The most popular use is for building single pane layouts for phones and multipane layouts for tablets (large screens). Fragment was introduced in Android 3.0 API 11. Fragment can also be used for supporting different layouts for portrait and landscape orientations.
A fragment stops as activity stops, and is destroyed as activity is destroyed. The OnCreateView()
method is where the view UI is created via the inflate()
method call. Following is the screenshot of our application in landscape orientation from our previous code:
We will make use of fragment to define a landscape layout for our DistanceConverter...