【鸿蒙Harmony】Fraction判断类似Fragment.
鸿蒙Fraction没有找到类似安卓Fragment.isAdded()的方法,本方法不是唯一的解决办法,经测试能用
private Fraction mCurFraction ;//当前已经显示的Fraction
public void createPageInContainer(ComponentContainer container, intposition) {
// 开启事务
FractionScheduler fractionScheduler = mFractionManager.startFractionScheduler();
if(mCurFraction != null) {
// 当前的fraction不为空,就隐藏
fractionScheduler.hide(mCurFraction);
}
String tag = container.getId() + ":"+ position;
Fraction fraction;
// 根据标签从FractionManager里面获取fraction
Optional<Fraction> fractionOptional = mFractionManager.getFractionByTag(tag);
if(fractionOptional.isPresent()) {
fraction = fractionOptional.get();
// 获取的fraction不为空,显示出来
fractionScheduler.show(fraction);
} else{
// 获取的fraction为空,创建fraction
fraction = getPage(position);
// 将fraction添加到fractionScheduler
fractionScheduler.add(container.getId(), fraction, tag);
}
mCurFraction = fraction;
// 提交事务
fractionScheduler.submit();
}