Fragments in android
By Michael Sadgun Rao Kona,
Fragment
Source:- https://www.tutorialspoint.com/android/android_fragments.htm
modularity
Example - app that responds to various screen sizes.
Requirement:
On larger screens, the app should display a static navigation drawer and a list in a grid layout.
On smaller screens, the app should display a bottom navigation bar and a list in a linear layout.
Solution:
The activity is then responsible for displaying the correct navigation UI while the fragment displays the list with the proper layout.
CREATE FRAGMENT
Different fragment classes available
Using a fragment
or
ADD a fragment via xml
Source:-https://developer.android.com/guide/fragments/create#java
ADD a fragment via xml
Source:-https://developer.android.com/guide/fragments/create#java
Add a fragment programmatically
Source:-https://developer.android.com/guide/fragments/create#java
Add a fragment programmatically
Source:-https://developer.android.com/guide/fragments/create#java
XML Code ( with no "name" attribute)
Source:-https://developer.android.com/guide/fragments/create#java
Java code to add fragment into an activity
Source:-https://developer.android.com/guide/fragments/create#java
Fragment transactions
Source:- https://google-developer-training.github.io/android-developer-advanced-course-concepts/unit-1-expand-the-user-experience/lesson-1-fragments/1-1-c-fragments/1-1-c-fragments.html#creating
if (savedInstanceState == null)
Passing arguments to fragment through Bundle
ViewGroup ID of the fragment
Name of the Fragment class
To pass arguments or input to fragment
To read arguments
Fragment life cycle
Fragment life cycle states
Fragment life cycle states
Activity State | Fragment Callbacks Triggered | Fragment Lifecycle |
Created | Fragment is added and its layout is inflated. | |
Started | Fragment is active and visible. | |
Resumed | Fragment is active and ready for user interaction. | |
Paused | Fragment is paused because the Activity is paused. | |
Stopped | Fragment is stopped and no longer visible. | |
Destroyed | Fragment is destroyed. |
Fragment lifecycle callbacks
Fragment Lifecycle states and their relation both the fragment's lifecycle callbacks and the fragment's view Lifecycle.
Fragment call backs
Fragment call backs
Fragment call backs