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
Hướng Dẫn Chi Tiết Xử Lý Lỗi E-66 Máy Giặt Electrolux
Mục ChínhHướng Dẫn Chi Tiết Xử Lý Lỗi E-66 Máy Giặt ElectroluxLỗi E-66 máy giặt Electrolux là gì?4 Nguyên nhân gây lỗi E-66 máy giặt…
Tủ Lạnh Sharp Lỗi H-36 Cách Xử Lý Đơn Giản
Mục ChínhTủ Lạnh Sharp Lỗi H-36 Cách Xử Lý Đơn GiảnGiới thiệu về lỗi H-36 trên tủ lạnh SharpNguyên nhân gây lỗi H-36 trên tủ…
Khắc phục lỗi E-62 máy giặt Electrolux không cần thợ
Mục ChínhKhắc phục lỗi E-62 máy giặt Electrolux không cần thợMã lỗi E-62 Máy giặt Electrolux là gì?Các bộ phận liên quan đến mã lỗi…
Tủ Lạnh Sharp Lỗi H-35 Nguy Cơ Không Thể Sửa Chữa!
Mục ChínhQuy Trình Tự Sửa Lỗi H-35 Tủ Lạnh Sharp Từng Bước An ToànMã lỗi H-35 trên tủ lạnh Sharp là gì?Nguyên nhân gây lỗi…
Tủ lạnh Sharp nháy lỗi H-34 Cuộc chạy đua với thời gian!
Mục ChínhTủ lạnh Sharp nháy lỗi H-34 Cuộc chạy đua với thời gian!Tìm Hiểu Lỗi H-34 Trên Tủ Lạnh Sharp Là Gì?Nguyên nhân phổ biến…
Hậu quả nghiêm trọng từ lỗi H-30 trên tủ lạnh Sharp
Mục ChínhHậu quả nghiêm trọng từ lỗi H-30 trên tủ lạnh SharpLỗi H-30, H-31, H-32, H-33 tủ Lạnh Sharp là gì?Tầm quan trọng của các…