鴻蒙Fraction沒有找到類似安卓Fragment.isAdded()的方法衷模,本方法不是唯一的解決辦法,經(jīng)測試能用
private Fraction mCurFraction?;//當前已經(jīng)顯示的Fraction?
????public void createPageInContainer(ComponentContainer?container,?intposition)?{
????????//?開啟事務(wù)
????????FractionScheduler?fractionScheduler?=?mFractionManager.startFractionScheduler();
????????if(mCurFraction?!=?null)?{
????????????//?當前的fraction不為空蒲赂,就隱藏
????????????fractionScheduler.hide(mCurFraction);
????????}
????????String?tag?=?container.getId()?+?":"+?position;
????????Fraction?fraction;
????????//?根據(jù)標簽從FractionManager里面獲取fraction
????????Optional<Fraction>?fractionOptional?=?mFractionManager.getFractionByTag(tag);
????????if(fractionOptional.isPresent())?{
????????????fraction?=?fractionOptional.get();
????????????//?獲取的fraction不為空阱冶,顯示出來
????????????fractionScheduler.show(fraction);
????????}?else{
????????????//?獲取的fraction為空,創(chuàng)建fraction
????????????fraction?=?getPage(position);
????????????//?將fraction添加到fractionScheduler
????????????fractionScheduler.add(container.getId(),?fraction,?tag);
????????}
????????mCurFraction?=?fraction;
????????//?提交事務(wù)
????????fractionScheduler.submit();
????}