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.

转载于:https://www.cnblogs.com/yushilong/p/3979602.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值