Fragment

34 篇文章 0 订阅
1. android/app/Fragment

官方SDK文档

Known direct subclasses
DialogFragment, ListFragment, PreferenceFragment WebViewFragment

This class was deprecated in API level 28.
Use the Support Library support/v4/app/Fragment for consistent behavior across all devices and access to Lifecycle.

与Fragment打交道的就是:FragmentManager

Fragment的生命周期

看这段就可以了

A Fragment is closely tied to the Activity it is in, and can not be used apart from one. Though Fragment defines its own lifecycle, that lifecycle is dependent on its activity: if the activity is stopped, no fragments inside of it can be started; when the activity is destroyed, all fragments will be destroyed

All subclasses of Fragment must include a public no-argumentconstructor

  1. onAttach(Activity) called once the fragment is associated with its activity.

  2. onCreate(Bundle) called to do initial creation of the fragment.

  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.

  4. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity#onCreate.

  5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.

  6. onStart() makes the fragment visible to the user (based on its containing activity being started).

  7. onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).

As a fragment is no longer being used, it goes through a reverse series of callbacks:

  1. onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.

  2. onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.

  3. onDestroyView() allows the fragment to clean up resources associated with its View.

  4. onDestroy() called to do final cleanup of the fragment’s state.

  5. onDetach() called immediately prior to the fragment no longer being associated with its activity.

Fragment加载到页面2种方式
  1. An activity’s layout XML can include tags to embed fragment instances inside of the layout. For example, here is a simple layout that embeds one fragment:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">
    
    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent" 
            android:layout_height="match_parent" />
</FrameLayout>
  1. 另外一种代码中 replace 或 add 添加

2. support/v4/app/Fragment

Static library support version of the framework’s Fragment. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used

The main differences when using this support version instead of the framework version are:

  1. Your activity must extend FragmentActivity

  2. You must call getSupportFragmentManager() to get the FragmentManager

嵌套Fragments Nested Fragments 是Android 4.2 API 17 引入的.
目的: 进一步增强动态复用.

Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically.


3. Fragment使用常见问题
  1. Fragment嵌套Fragment,一定使用 getChildFragmentManager()。From within a nested fragment, you can get a reference to the parent fragment by calling getParentFragment().
  2. commit 和 commitAllowingStateLoss 区别
    Fragment所在Activity状态恢复时的区别,具体细节请参考:Android commit 和 commitAllowingStateLoss 的区别
  3. Fragment的Back Stack管理
  4. replace 和 add 使用区别
    一种是add方式来进行show和add,这种方式你切换fragment不会让fragment重新刷新,而用replace方式会使fragment重新刷新,因为add方式是将fragment隐藏了而不是销毁再创建,replace方式每次都是重新创建
参考
  1. Fragment里面嵌套Fragment的问题
  2. Fragment嵌套Fragment中getChildFragmentManager的问题
  3. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值