Fragment在android3.0
(api11
)开始才有的,刚开始是为了适配平板而推出(现在用处很多),现在在Android日常开发中被越来越多的使用,Fragment不能独立存在
它必须依附于四大组件之一Activity
(将此activity称为宿主activity
)而存在,同时activity必须是FragmentActivity
及其子类(所以我们直接继承AppCompatActivity (extends FragmentActivity
)同样可以使用Fragment)
-
Fragment是Object类的子类
public class Fragment extends Object implements ComponentCallbacks2, View.OnCreateContextMenuListener
-
Fragment的子类有:
DialogFragment,ListFragment,PreferenceFragment,WebViewFragment -
使用Fragment需要使用FragmentManager来进行管理Activity.getFragmentManager,Fragment.getFragmentManager
-
Lifecycle(生命周期)
由于它的使用依赖于宿主activity,所以fragment的生命周期与宿主Activity的生命周期息息相关 -
当宿主activity执行到生命周期的onResume()时,Fragment的生命周期方法依次执行以下方法来源于官网
- 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()](https://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle))
. - 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).
-
当fragment不再被使用,将执行以下方法
- 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.
-
启动一个Activity时,Fragment与Activity的生命周期分别执行的方法如下
1、fragment的生命周期包含在整个宿主activity的方法中
2、fragment的创建在整个activity的创建完成之前,fragment的销毁在activity销毁之前
3、相同生命周期方法,fragment先执行与宿主activity
4、无论宿主activity中有多少fragment,当activity销毁时,所有的fragment均会销毁

- Fragment的使用方式
getFragmentManager().beginTransaction().add(containerId, fragment实例对象).commit();
- 静态加载
在activity的layout布局文件中使用,fragment
标签并设置name
属性,在启动activity不需要添加额外代码即可加载fragment
<fragment
android:name="FragmentTwo所在包.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
- 动态加载
在Activity的layout中不使用fragment标签
,使用一个容器(比如:FrameLayout
、RelativeLayout
等)替代
/**
* activity容器
*/
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" />
- 使用方法1:
fragmentManager为Activity的方法,FragmentOne 继承自app包下Fragment
fragmentManager.beginTransaction().replace(R.id.fragment_container, FragmentOne()).commit()
- 使用方法2:
(supportFragmentManager为FragmentActivity中方法,FragmentTwo为v4包下的framgnet)
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, FragmentTwo()).commit()
备注:动态添加多个fragment时,每次都需要开启一个新事务beginTransaction,因为每次执行commit后均会置空,所以FragmentTransaction 不能定义为全局变量,否则程序crash
- Fragment的数据传递
//设置数据源
fragment.setArguments(Bundle bundle)
//获取数据
Bundle bundle=getArguments();
-
Fragment
与Activity
的通信方式
fragment是依附于activity而存在的,所以能直接获取彼此的实例对象(适用于activity中动态添加fragment的情况)-
fragment
中获取activity的某个属性或方法:在activity中将这个属性或方法定义为public类型
,在fragment中通过((宿主Activity)getActivity).属性(方法)
来获取,注意进行非空判断,activity
中获取fragment的属性或方法通过fragment的实例对象来获取即可(只需要在framgnet中将 对应的声明为public
类型即可) - 其他:通过广播
BroadcastReceiver
,回调方法
,EventBus
等
-
-
Fragment与Fragment的通信方式
通过宿主activity来实现通信
,BroadcastReceiver
全局变量
等 -
Back Stack
// on to the back stack.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);//添加到回退栈
ft.commit();
- Fragment设置动画
网友评论