FRAGEMNTS NAME :- AKLANTA BHUKTA FRAGMENTS NAME :- DEBASHSISH PAL SEC:- C REGD.NO :- 220301120170 BRANCH :- BTECH(CSE)
INTRODUCTION :- -> A Fragment is a reusable portion of an app's user interface (UI) that can be placed within an Activity. -> It serves as a modular section, allowing each fragment to have its own layout and lifecycle. PURPOSE :- Flexible and Dynamic UI Design : Fragments allow you to create a more adaptable interface, especially useful for devices with varying screen sizes, like tablets and phones. Code Reusability : With fragments, you can reuse components across multiple activities, reducing redundancy in code.
USES OF FRAGEMTS :- Adaptability for Multiple Screens : Fragments help create dynamic layouts for different screen sizes, such as phones and tablets. This flexibility is key for providing a consistent user experience on various devices.
CREATING A FRAGMENT :- Create a class that extend Fragment class. In the fragment class, override the onCreateView () method to specify the layout for the fragment. import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup i mport androidx.fragment.app.Fragment class ExampleFragment : Fragment () { override fun onCreateView ( inflater: LayoutInflater , container: ViewGroup ?, savedInstanceState : Bundle ? ): View? { // Inflate the layout for this fragment return inflater.inflate ( R.layout.fragment_example , container, false ) } } CODE:-
FRAGMENT LIFECYCLE:- onAttach () : Called when the fragment is attached to its host activity. onCreate () : Initializes non-UI components, similar to onCreateView () : Creates and returns the view hierarchy associated with the fragment’s UI. onActivityCreated () : Called after the activity’s
onStart () and onResume () : Called as the fragment becomes visible and active to the user. onPause () and onStop () : Called as the fragment goes into the background or is no longer visible. onDestroyView () : Cleans up resources related to the fragment’s view. onDestroy () : Final cleanup, similar to activity’s onDestroy (). onDetach () : Called when the fragment is disassociated from its activity.
TYPES OF FRAGMENTS :- 1. ListFragment :- A fragment that displays a list of items managed by an adapter (like a RecyclerView ). Simplifies creating list-based UIs within fragments. 2. DialogFragment :- A fragment that displays a floating dialog window. Allows creating custom dialogs that are more flexible than standard alert dialogs. 3. PreferenceFragment :- A fragment specifically for managing user preferences. Provides a standard way to implement settings screens using XML-based preference definitions.