彻底了解Android开发中Fragment的知识

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的生命周期方法依次执行以下方法来源于官网

    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()](https://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)).
    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).
  • 当fragment不再被使用,将执行以下方法

    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.
  • 启动一个Activity时,Fragment与Activity的生命周期分别执行的方法如下

1、fragment的生命周期包含在整个宿主activity的方法中
2、fragment的创建在整个activity的创建完成之前,fragment的销毁在activity销毁之前
3、相同生命周期方法,fragment先执行与宿主activity
4、无论宿主activity中有多少fragment,当activity销毁时,所有的fragment均会销毁

启动/销毁宿主Activity时Fragment的生命周期方法执行顺序(静态加载方式)
  • Fragment的使用方式
getFragmentManager().beginTransaction().add(containerId, fragment实例对象).commit();
  • 静态加载
    在activity的layout布局文件中使用,fragment标签并设置name属性,在启动activity不需要添加额外代码即可加载fragment
 
  • 动态加载
    在Activity的layout中不使用fragment标签,使用一个容器(比如:FrameLayoutRelativeLayout等)替代
/** 
* activity容器
*/
 
  • 使用方法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();
  • FragmentActivity的通信方式
    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设置动画

你可能感兴趣的:(彻底了解Android开发中Fragment的知识)