1.fragment必须要有唯一标识,在xml文件中指定id或者tag
2.在xml文件中必须指定class
3.生命周期与Activity共存如下图
4.可以使用FragmentTransaction在一个activity之中添加、删除和替换Fragment
public void addFragment(View view) {
ft = fm.beginTransaction();
ft.add(R.id.fl_group, new ButtonFragment(),"fg_button");
ft.addToBackStack(null);
ft.commit();
}
public void removeFragment(View view) {
ft = fm.beginTransaction();
Fragment fragment = fm.findFragmentByTag("fg_button");
ft.remove(fragment);
ft.commit();
}
public void replaceFragment(View view) {
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fl_group, new TextViewFragment(), "fg_textView");
ft.commit();
}
5.将添加、删除和替换的Fragment加入到Fragment back stack
ft = fm.beginTransaction();
ft.add(R.id.fl_group, new ButtonFragment(),"fg_button");
String tag = "fg_button";
ft.addToBackStack(tag);
ft.commit();
6.默认的过渡动画:ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_*);
7.Fragment与activity之间的通信
Activity继承接口,进行扩展,Fragment调用接口方法,最终由Activity内的方法执行。