fix bugs.8
This commit is contained in:
@@ -184,37 +184,40 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放所有子Fragment
|
||||
* 安全释放所有子 Fragment
|
||||
*/
|
||||
private void releaseChildFragments() {
|
||||
try {
|
||||
// 获取所有子Fragment
|
||||
List<Fragment> fragments = fragmentManager.getFragments();
|
||||
FragmentManager fm = getChildFragmentManager();
|
||||
List<Fragment> fragments = fm.getFragments();
|
||||
if (!fragments.isEmpty()) {
|
||||
for (Fragment fragment : fragments) {
|
||||
if (fragment != null) {
|
||||
// 检查Fragment是否实现了特定的销毁接口
|
||||
if (fragment instanceof SingSongFragment) {
|
||||
((SingSongFragment) fragment).releaseResources();
|
||||
singSongFragment = null;
|
||||
} else if (fragment instanceof RoomKtvFragment) {
|
||||
((RoomKtvFragment) fragment).releaseResources();
|
||||
ktvFragment = null;
|
||||
} else if (fragment instanceof FriendshipRoomFragment) {
|
||||
((FriendshipRoomFragment) fragment).releaseResources();
|
||||
friendshipRoomFragment = null;
|
||||
} else if (fragment instanceof RoomCabinFragment) {
|
||||
((RoomCabinFragment) fragment).releaseResources();
|
||||
roomCabinFragment = null;
|
||||
} else if (fragment instanceof RoomAuctionFragment) {
|
||||
((RoomAuctionFragment) fragment).releaseResources();
|
||||
roomAuctionFragment = null;
|
||||
} else if (fragment instanceof RoomJukeboxFragment) {
|
||||
roomJukeboxFragment = null;
|
||||
}else if (fragment instanceof RoomMentorShipFragment){
|
||||
mentorShipFragment = null;
|
||||
}
|
||||
if (fragment == null) continue;
|
||||
|
||||
// 调用各子 Fragment 的释放方法
|
||||
if (fragment instanceof SingSongFragment) {
|
||||
((SingSongFragment) fragment).releaseResources();
|
||||
singSongFragment = null;
|
||||
} else if (fragment instanceof RoomKtvFragment) {
|
||||
((RoomKtvFragment) fragment).releaseResources();
|
||||
ktvFragment = null;
|
||||
} else if (fragment instanceof FriendshipRoomFragment) {
|
||||
((FriendshipRoomFragment) fragment).releaseResources();
|
||||
friendshipRoomFragment = null;
|
||||
} else if (fragment instanceof RoomCabinFragment) {
|
||||
((RoomCabinFragment) fragment).releaseResources();
|
||||
roomCabinFragment = null;
|
||||
} else if (fragment instanceof RoomAuctionFragment) {
|
||||
((RoomAuctionFragment) fragment).releaseResources();
|
||||
roomAuctionFragment = null;
|
||||
} else if (fragment instanceof RoomJukeboxFragment) {
|
||||
roomJukeboxFragment = null;
|
||||
} else if (fragment instanceof RoomMentorShipFragment) {
|
||||
mentorShipFragment = null;
|
||||
}
|
||||
|
||||
// 安全 remove
|
||||
fm.beginTransaction().remove(fragment).commitNowAllowingStateLoss();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -363,72 +366,76 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
|
||||
performLoadSubFragment(qxRoomSeatViewType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全加载子 Fragment(先释放再加载)
|
||||
*/
|
||||
private void performLoadSubFragment(QXRoomSeatViewType qxRoomSeatViewType) {
|
||||
|
||||
// 释放子Fragment
|
||||
releaseChildFragments();
|
||||
this.qxRoomSeatViewType = qxRoomSeatViewType;
|
||||
if (mRoomInfoResp == null || mRoomInfoResp.getRoom_info() == null) return;
|
||||
|
||||
// 先释放旧 Fragment
|
||||
releaseChildFragments();
|
||||
|
||||
String typeId = mRoomInfoResp.getRoom_info().getType_id();
|
||||
String labelId = mRoomInfoResp.getRoom_info().getLabel_id();
|
||||
|
||||
Fragment newFragment = null;
|
||||
mRoomActivity.isInAuctionTopBtVisible(false);
|
||||
|
||||
RoomType roomType = RoomType.Companion.fromTypeId(typeId);
|
||||
|
||||
if (roomType == RoomType.MUTUAL_ENTERTAINMENT) {
|
||||
if (friendshipRoomFragment == null) {
|
||||
friendshipRoomFragment = FriendshipRoomFragment.newInstance(mRoomInfoResp);
|
||||
} else {
|
||||
friendshipRoomFragment.roomInfoUpdate(mRoomInfoResp);
|
||||
}
|
||||
} else friendshipRoomFragment.roomInfoUpdate(mRoomInfoResp);
|
||||
newFragment = friendshipRoomFragment;
|
||||
|
||||
} else if (roomType == RoomType.AUCTION) {
|
||||
mRoomActivity.isInAuctionTopBtVisible(true);
|
||||
if (roomAuctionFragment == null)
|
||||
roomAuctionFragment = RoomAuctionFragment.newInstance(mRoomInfoResp);
|
||||
else {
|
||||
roomAuctionFragment.roomInfoUpdate(mRoomInfoResp);
|
||||
}
|
||||
else roomAuctionFragment.roomInfoUpdate(mRoomInfoResp);
|
||||
newFragment = roomAuctionFragment;
|
||||
|
||||
} else if (roomType == RoomType.DATING) {
|
||||
newFragment = switch (labelId) {
|
||||
case "2" -> {
|
||||
if (ktvFragment == null)
|
||||
ktvFragment = RoomKtvFragment.newInstance(mRoomInfoResp);
|
||||
if (ktvFragment == null) ktvFragment = RoomKtvFragment.newInstance(mRoomInfoResp);
|
||||
else ktvFragment.roomInfoUpdate(mRoomInfoResp);
|
||||
yield ktvFragment;
|
||||
}
|
||||
case "1" -> {
|
||||
if (singSongFragment == null)
|
||||
singSongFragment = SingSongFragment.newInstance(mRoomInfoResp);
|
||||
else
|
||||
singSongFragment.roomInfoUpdate(mRoomInfoResp);
|
||||
else singSongFragment.roomInfoUpdate(mRoomInfoResp);
|
||||
yield singSongFragment;
|
||||
}
|
||||
default -> newFragment;
|
||||
};
|
||||
|
||||
} else if (roomType == RoomType.BLACK_ROOM) {
|
||||
if (roomCabinFragment == null)
|
||||
roomCabinFragment = RoomCabinFragment.newInstance(mRoomInfoResp);
|
||||
else roomCabinFragment.roomInfoUpdate(mRoomInfoResp);
|
||||
newFragment = roomCabinFragment;
|
||||
setviewyc();
|
||||
|
||||
} else if (roomType == RoomType.JUKEBOX) {
|
||||
if (roomJukeboxFragment == null)
|
||||
roomJukeboxFragment = RoomJukeboxFragment.newInstance(mRoomInfoResp);
|
||||
else roomJukeboxFragment.roomInfoUpdate(mRoomInfoResp);
|
||||
newFragment = roomJukeboxFragment;
|
||||
|
||||
} else if (roomType == RoomType.SIGN_CONTRACT) {
|
||||
if (mentorShipFragment == null) {
|
||||
mentorShipFragment = RoomMentorShipFragment.Companion.newInstance(mRoomInfoResp);
|
||||
} else {
|
||||
mentorShipFragment.roomTypeSwitch(mRoomInfoResp);
|
||||
}
|
||||
} else mentorShipFragment.roomTypeSwitch(mRoomInfoResp);
|
||||
newFragment = mentorShipFragment;
|
||||
}
|
||||
|
||||
if (newFragment != null) {
|
||||
performFragmentReplacement(newFragment, R.id.container);
|
||||
switchFragmentSafely(newFragment, R.id.container);
|
||||
} else {
|
||||
LogUtils.e("newFragment==null");
|
||||
}
|
||||
@@ -443,11 +450,72 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Fragment currentFragment = null;
|
||||
private boolean isReplacing = false;
|
||||
|
||||
private void switchFragmentSafely(Fragment newFragment, int containerId) {
|
||||
if (isReplacing) return;
|
||||
isReplacing = true;
|
||||
|
||||
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
|
||||
|
||||
// 1️⃣ 如果 newFragment 没有添加过,先 add
|
||||
if (!newFragment.isAdded()) {
|
||||
ft.add(containerId, newFragment, newFragment.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
// 2️⃣ 显示 newFragment
|
||||
ft.show(newFragment);
|
||||
|
||||
// 3️⃣ 提交事务立即执行,确保 UI 已渲染
|
||||
ft.commitNowAllowingStateLoss();
|
||||
|
||||
// 4️⃣ 移除或隐藏旧 Fragment(currentFragment)
|
||||
if (currentFragment != null && currentFragment != newFragment) {
|
||||
FragmentTransaction removeFt = getChildFragmentManager().beginTransaction();
|
||||
removeFt.remove(currentFragment); // 或 remove + releaseResources()
|
||||
removeFt.commitNowAllowingStateLoss();
|
||||
}
|
||||
|
||||
// 5️⃣ 更新 currentFragment
|
||||
currentFragment = newFragment;
|
||||
isReplacing = false;
|
||||
}
|
||||
|
||||
|
||||
// RoomFragment.java
|
||||
|
||||
|
||||
/**
|
||||
* 安全替换子 Fragment
|
||||
*/
|
||||
private void performFragmentReplacement(@NonNull Fragment newFragment, int containerId) {
|
||||
ensureFragmentView(newFragment);
|
||||
fragmentTransaction.replace(containerId, newFragment); // 加TAG便于查找
|
||||
fragmentTransaction.commitAllowingStateLoss();
|
||||
fragmentManager.executePendingTransactions(); // 关键
|
||||
if (isReplacing) return; // 防重复提交
|
||||
isReplacing = true;
|
||||
|
||||
// 在 UI 线程下一帧执行,保证上一帧 remove 完成
|
||||
if (getView() != null) {
|
||||
getView().post(() -> {
|
||||
if (!isAdded()) {
|
||||
isReplacing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
|
||||
ft.replace(containerId, newFragment, "TAG_" + containerId);
|
||||
ft.commitNowAllowingStateLoss();
|
||||
|
||||
isReplacing = false; // 释放标记
|
||||
});
|
||||
} else {
|
||||
// Fragment view 尚未创建,延迟执行
|
||||
getViewLifecycleOwnerLiveData().observe(getViewLifecycleOwner(), owner -> {
|
||||
if (owner != null) {
|
||||
performFragmentReplacement(newFragment, containerId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -167,6 +167,7 @@
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@id/ease_container"/>
|
||||
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/ease_container"
|
||||
android:layout_width="0dp"
|
||||
|
||||
Reference in New Issue
Block a user