1. android/app/Fragment
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-argument
constructor
-
onAttach(Activity) called once the fragment is associated with its activity.
-
onCreate(Bundle) called to do initial creation of the fragment.
-
onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
-
onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity#onCreate.
-
onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
-
onStart() makes the fragment visible to the user (based on its containing activity being started).
-
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:
-
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.
-
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.
-
onDestroyView() allows the fragment to clean up resources associated with its View.
-
onDestroy() called to do final cleanup of the fragment’s state.
-
onDetach() called immediately prior to the fragment no longer being associated with its activity.
Fragment加载到页面2种方式
- 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>
- 另外一种代码中 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:
-
Your activity must extend FragmentActivity
-
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使用常见问题
- Fragment嵌套Fragment,一定使用
getChildFragmentManager()
。From within a nested fragment, you can get a reference to the parent fragment by callinggetParentFragment()
. - commit 和 commitAllowingStateLoss 区别
Fragment所在Activity状态恢复时的区别,具体细节请参考:Android commit 和 commitAllowingStateLoss 的区别 - Fragment的Back Stack管理
- replace 和 add 使用区别
一种是add方式来进行show和add,这种方式你切换fragment不会让fragment重新刷新,而用replace方式会使fragment重新刷新,因为add方式是将fragment隐藏了而不是销毁再创建,replace方式每次都是重新创建