Create A Fragment - Android Developers
Create A Fragment - Android Developers
com/guide/fragments/create
(#groovy)
(#kotlin)
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
...
}
}
To include the AndroidX Fragment library to your project, add the following dependencies
in your app's �le:
(#groovy)
(#kotlin)
dependencies {
val fragment_version = "1.8.5"
1 of 6 11/16/24, 08:58
Create a fragment | Android Developers https://developer.android.com/guide/fragments/create
implementation("androidx.fragment:fragment-ktx:$fragment_version")
}
(#java)
(#kotlin)
The Fragment library also provides more specialized fragment base classes:
(/reference/androidx/fragment/app/DialogFragment)
Displays a �oating dialog. Using this class to create a dialog is a good alternative to
using the dialog helper methods in the (/reference/android/app/Activity) class,
as fragments automatically handle the creation and cleanup of the . See
Displaying dialogs with (/guide/fragments/dialogs) for more details.
(/reference/androidx/preference/PreferenceFragmentCompat)
2 of 6 11/16/24, 08:58
Create a fragment | Android Developers https://developer.android.com/guide/fragments/create
You can add your fragment to the activity's view hierarchy either by de�ning the fragment
in your activity's layout �le or by de�ning a fragment container in your activity's layout �le
and then programmatically adding the fragment from within your activity. In either case,
you need to add a
(/reference/androidx/fragment/app/FragmentContainerView) that de�nes the location where the
fragment should be placed within the activity's view hierarchy. It is strongly
recommended to always use a as the container for fragments,
as includes �xes speci�c to fragments that other view groups
such as do not provide.
Note: You can use the a�ribute instead of as an alternative way to specify which
to instantiate.
3 of 6 11/16/24, 08:58
Create a fragment | Android Developers https://developer.android.com/guide/fragments/create
To programmatically add a fragment to your activity's layout, the layout should include a
to serve as a fragment container, as shown in the following
example:
While your activity is running, you can make fragment transactions such as adding,
removing, or replacing a fragment. In your , you can get an instance of
the (/reference/androidx/fragment/app/FragmentManager), which can be
used to create a . Then, you can instantiate your fragment within
your activity's method using
(/reference/androidx/fragment/app/FragmentTransaction#add(int,%20java.lang.Class%3C?
%20extends%20androidx.fragment.app.Fragment%3E,%20android.os.Bundle))
, passing in the ID of the container in your layout and the fragment class you
want to add and then commit the transaction, as shown in the following example:
(#java)
(#kotlin)
4 of 6 11/16/24, 08:58
Create a fragment | Android Developers https://developer.android.com/guide/fragments/create
In the previous example, note that the fragment transaction is only created when
is . This is to ensure that the fragment is added only once,
when the activity is �rst created. When a con�guration change occurs and the activity is
recreated, is no longer , and the fragment does not need to be
added a second time, as the fragment is automatically restored from the
.
If your fragment requires some initial data, arguments can be passed to your fragment by
providing a (h�ps://developer.android.com/reference/android/os/Bundle) in the call to
, as shown below:
(#java)
(#kotlin)
The arguments can then be retrieved from within your fragment by calling
(/reference/androidx/fragment/app/Fragment#requireArguments()), and the
appropriate ge�er methods can be used to retrieve each argument.
(#kotlin)
(#java)
5 of 6 11/16/24, 08:58
Create a fragment | Android Developers https://developer.android.com/guide/fragments/create
Content and code samples on this page are subject to the licenses described in the Content License
(/license). Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its a�liates.
6 of 6 11/16/24, 08:58