Create a fragment | Android Developers
This document describes how to create a fragment and include it in an activity .
Mục Chính
Setup your environment
Fragments require a dependency on the
AndroidX Fragment library. You need to
add the Google Maven repository
to your project’s settings.gradle
file in order to include this dependency.
Groovy
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() ... } }
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’sbuild.gradle
file:Bạn đang đọc: Create a fragment | Android Developers
Groovy
dependencies { def fragment_version = "1.5.7" // Java language implementation implementation "androidx.fragment:fragment:$fragment_version" // Kotlin implementation "androidx.fragment:fragment-ktx:$fragment_version" }Kotlin
dependencies { val fragment_version = "1.5.7" // Java language implementation implementation("androidx.fragment:fragment:$fragment_version") // Kotlin implementation("androidx.fragment:fragment-ktx:$fragment_version") }Create a fragment class
To create a fragment, extend the AndroidX
Fragment
class, and override
its methods to insert your app logic, similar to the way you would create
anActivity
class. To create a minimal
fragment that defines its own layout, provide your fragment’s layout resource
to the base constructor, as shown in the following example:Kotlin
class ExampleFragment : Fragment(R.layout.example_fragment)Java
class ExampleFragment extends Fragment { public ExampleFragment() { super(R.layout.example_fragment); } }The Fragment library also provides more specialized fragment base classes :
DialogFragment
- Displays a floating dialog. Using this class to create a dialog is a good
alternative to using the dialog helper methods in the
Activity
class, as fragments
automatically handle the creation and cleanup of theDialog
.
See Displaying dialogs withDialogFragment
for more details.PreferenceFragmentCompat
- Displays a hierarchy of
Preference
objects as a
list. You can usePreferenceFragmentCompat
to
create a settings screen for your app.Add a fragment to an activity
Generally, your fragment must be embedded within an AndroidX
FragmentActivity
to
contribute a portion of UI to that activity’s layout.FragmentActivity
is the base class for
AppCompatActivity
,
so if you’re already subclassingAppCompatActivity
to provide backward
compatibility in your app, then you do not need to change your activity
base class.You can add your fragment to the activity’s view hierarchy either by
defining the fragment in your activity’s layout file or by defining a
fragment container in your activity’s layout file and then
programmatically adding the fragment from within your activity. In either case, you need
to add a
FragmentContainerView
that defines the location where the fragment should be placed within the
activity’s view hierarchy. It is strongly recommended to always use a
FragmentContainerView
as the container for fragments, as
FragmentContainerView
includes fixes specific to fragments that other
view groups such asFrameLayout
do not provide.Add a fragment via XML
To declaratively add a fragment to your activity layout’s XML, use a
FragmentContainerView
element.Here’s an example activity layout containing a single
FragmentContainerView
:
The
android:name
attribute specifies the class name of theFragment
to
instantiate. When the activity’s layout is inflated, the specified fragment
is instantiated,
onInflate()
is called on the newly instantiated fragment, and aFragmentTransaction
is created to add the fragment to theFragmentManager
.Xem thêm: Định vị xe máy BK88M
Note:
You can use the
class
attribute instead ofandroid:name
as an
alternative way to specify whichFragment
to instantiate.Add a fragment programmatically
To programmatically add a fragment to your activity’s layout, the layout
should include aFragmentContainerView
to serve as a fragment container,
as shown in the following example:
Unlike the XML approach, the
android:name
attribute isn’t used on the
FragmentContainerView
here, so no specific fragment is automatically
instantiated. Instead, a
FragmentTransaction
is used to instantiate a fragment and add it to the activity’s layout.While your activity is running, you can make fragment transactions such as
adding, removing, or replacing a fragment. In yourFragmentActivity
, you can
get an instance of the
FragmentManager
, which
can be used to create aFragmentTransaction
. Then, you can instantiate your
fragment within your activity’sonCreate()
method using
FragmentTransaction.add()
,
passing in theViewGroup
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:Kotlin
class ExampleActivity : AppCompatActivity(R.layout.example_activity) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (savedInstanceState == null) { supportFragmentManager.commit { setReorderingAllowed(true) add(R.id.fragment_container_view) } } } } Java
public class ExampleActivity extends AppCompatActivity { public ExampleActivity() { super(R.layout.example_activity); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .setReorderingAllowed(true) .add(R.id.fragment_container_view, ExampleFragment.class, null) .commit(); } } }Note:always use
setReorderingAllowed(true)
when
performing aFragmentTransaction
. For more information on reordered
transactions, see
You shouldusewhen performing a. For more information on reordered transactions, see Fragment transactionsIn the previous example, note that the fragment transaction is only created
whensavedInstanceState
isnull
. This is to ensure that the fragment
is added only once, when the activity is first created. When a
configuration change occurs and the activity is recreated,
savedInstanceState
is no longernull
, and the fragment does not need
to be added a second time, as the fragment is automatically restored
from thesavedInstanceState
.If your fragment requires some initial data, arguments can be passed to your fragment by providing a
Bundle
in the call toFragmentTransaction.add()
, as shown below:Kotlin
class ExampleActivity : AppCompatActivity(R.layout.example_activity) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (savedInstanceState == null) { val bundle = bundleOf("some_int" to 0) supportFragmentManager.commit { setReorderingAllowed(true) add(R.id.fragment_container_view, args = bundle) } } } }Java
public class ExampleActivity extends AppCompatActivity { public ExampleActivity() { super(R.layout.example_activity); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { Bundle bundle = new Bundle(); bundle.putInt("some_int", 0); getSupportFragmentManager().beginTransaction() .setReorderingAllowed(true) .add(R.id.fragment_container_view, ExampleFragment.class, bundle) .commit(); } } }The arguments
Bundle
can then be retrieved from within your fragment by
calling
requireArguments()
,
and the appropriateBundle
getter methods can be used to retrieve
each argument.
Kotlin
class ExampleFragment : Fragment(R.layout.example_fragment) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val someInt = requireArguments().getInt("some_int") ... } }
Java
class ExampleFragment extends Fragment { public ExampleFragment() { super(R.layout.example_fragment); } @Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { int someInt = requireArguments().getInt("some_int"); ... } }
See also
Fragment transactions and the FragmentManager
are covered in more detail
in the Fragment manager guide.
Source: https://thomaygiat.com
Category : Ứng Dụng
Máy Giặt Electrolux Lỗi E51 Làm Tăng Nguy Cơ Hỏng Nặng
Mục ChínhMáy Giặt Electrolux Lỗi E51 Làm Tăng Nguy Cơ Hỏng NặngNguyên Nhân Máy Giặt Electrolux Báo Lỗi E511. Động Cơ Hỏng2. Mạch Điều Khiển…
Hậu quả từ lỗi H-29 tủ lạnh Sharp Side by Side
Mục ChínhHậu quả từ lỗi H-29 tủ lạnh Sharp Side by SideMã Lỗi H-29 Tủ Lạnh Sharp Là Gì?Tầm Quan Trọng Của Việc Khắc Phục…
Hỏi đáp giấy dán tường chống ẩm mốc
Mục ChínhGiải Mã 25+ Hỏi Đáp Giấy Dán Tường Chống Ẩm MốcChống ẩm mốc cùng giấy dán tường1. Nguyên nhân gây ẩm mốc trong không…
Máy Giặt Electrolux Lỗi E-45 Kiểm Tra Ngay!
Mục ChínhMáy Giặt Electrolux Lỗi E-45 Kiểm Tra Ngay!Định Nghĩa Mã Lỗi E-45 Máy Giặt ElectroluxNguyên nhân lỗi E-45 máy giặt Electrolux1. Cảm biến cửa…
Hướng dẫn sửa Tủ lạnh Sharp lỗi H-28 chi tiết và an toàn
Mục ChínhHướng dẫn sửa Tủ lạnh Sharp lỗi H-28 chi tiết và an toànLỗi H-28 Trên Tủ Lạnh Sharp Là Gì?Dấu Hiệu Nhận Biết Lỗi…
Máy giặt Electrolux gặp lỗi E-44 điều bạn nên làm
Mục ChínhMáy giặt Electrolux gặp lỗi E-44 điều bạn nên làmĐịnh nghĩa mã lỗi E-44 máy giặt Electrolux5 Nguyên nhân gây ra mã lỗi E-44…