android学习笔记:activity(6)

1.fragment表示应用界面中可重复使用的一部分。fragment定义和管理自己的布局,具有自己的生命周期,并且可以处理自己的输入事件。Fragment 不能独立存在,而是必须由 Activity 或另一个 Fragment 托管。Fragment 的视图层次结构会成为宿主的视图层次结构的一部分,或附加到宿主的视图层次结构。

2.当activity处于started生命周期状态或更高的状态时,可以添加、替换或移除fragment。可以将这些更改记录保留在由activity管理的返回栈中,从而允许撤销这些更改

3.可以在同一activity或多个activity中使用同一Fragment类的多个实例,甚至可以将其作为另一个fragment的子集。因此,应该只为fragment提供管理它自己的界面所需的逻辑。应该避免让一个fragment依赖于另一个fragment或从一个fragment操控另一个fragment

4.you can add your fragment to the activity's view hierarchy(层次体系) either by defining the fragment in your activity's in your activity's layout file or by defining a ftagment 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 fragmentcontainnerView includes fixes specific to fragment that other view groups such as frameLayout do not provide

        to declaratively(显示的) add a fragment to your activity layout's XML

<!-- res/layout/example_activity.xml -->
<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.ExampleFragment" />

        to programmatically add a fragment to your activity's layout,the layout should include a FragmentContainerView to serve as a fragment container

<!-- res/layout/example_activity.xml -->
<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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.Then, you can instantiate your fragment within your activity's onCreate() method using FragmentTransaction.add(),passing in the ViewGroup ID of the container in your layout and the fragment class you want to add and then commit the transaction

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<ExampleFragment>(R.id.fragment_container_view, args = bundle)
            }
        }
    }
}

The arguments Bundle can then be retrieved form within your fragment by calling requireArguments(),and the appropriate(合适的) Bundle getter methods can be used to retrieve each argument

class ExampleFragment : Fragment(R.layout.example_fragment) {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val someInt = requireArguments().getInt("some_int")
        ...
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值