Android Fragment小记

1、remove,add,replace

remove:移除fragment,被移除的fragment会销毁掉,包括实例和界面。

add:

add(int containerViewId, Fragment fragment)
Calls add(int, Fragment, String) with a null tag.

add(Fragment fragment, String tag)
Calls add(int, Fragment, String) with a 0 containerViewId.

add(int containerViewId, Fragment fragment, String tag)
Add a fragment to the activity state.

replace:等同于remove+add。

说明:往同一个containerViewId中replace会移除掉旧的fragment的界面,然后add新的fragment,只会有一个fragment的界面存在。

add会往同一个containerViewId中添加多个fragment,包括实例和界面,后加进去的会叠加在前面的fragment的上面,可以有多个fragment存在。

2、backstack fragment的后退栈

在调用add或者replace方法之后,调用commit方法之前加上addToBackStack会将当前的fragment加入到backstack。

3、清空backstack方法

popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)。

public static final int POP_BACK_STACK_INCLUSIVE

Added in API level 11
Flag for popBackStack(String, int) and popBackStack(int, int): If set, and the name or ID of a back stack entry has been supplied, then all matching entries will be consumed until one that doesn't match is found or the bottom of the stack is reached. Otherwise, all entries up to but not including that entry will be removed.

posted @ 2014-09-18 16:55  ysl_longer  阅读(325)  评论(0编辑  收藏  举报