diff --git a/MainModule/src/main/java/com/xscm/modulemain/activity/room/fragment/RoomFragment.java b/MainModule/src/main/java/com/xscm/modulemain/activity/room/fragment/RoomFragment.java index 2846964c..ebce9105 100644 --- a/MainModule/src/main/java/com/xscm/modulemain/activity/room/fragment/RoomFragment.java +++ b/MainModule/src/main/java/com/xscm/modulemain/activity/room/fragment/RoomFragment.java @@ -274,37 +274,37 @@ public class RoomFragment extends BaseMvpFragment heartLists) { - if (friendshipRoomFragment != null) { + if (friendshipRoomFragment == currentFragment) { friendshipRoomFragment.friendHeartNumberDidChanged(heartLists); } } /// 交友房麦位发生变化 public void friendSeatDidChanged(List pitArr) { - if (friendshipRoomFragment != null) { + if (friendshipRoomFragment == currentFragment) { friendshipRoomFragment.friendSeatDidChanged(pitArr); } } public void upFriendList(List pitArr) { - if (friendshipRoomFragment != null) { + if (friendshipRoomFragment == currentFragment) { friendshipRoomFragment.upFriendList(); } } /// 小黑屋修改倒计时 public void upCabinFragment(long time) { - if (roomCabinFragment != null) + if (roomCabinFragment == currentFragment) roomCabinFragment.upCabinFragment(time); } @@ -375,7 +375,7 @@ public class RoomFragment extends BaseMvpFragment { + if (!isAdded()) { + isReplacing = false; + return; + } - // 3️⃣ 提交事务立即执行,确保 UI 已渲染 - ft.commitNowAllowingStateLoss(); + FragmentTransaction ft = getChildFragmentManager().beginTransaction(); - // 4️⃣ 移除或隐藏旧 Fragment(currentFragment) - if (currentFragment != null && currentFragment != newFragment) { - FragmentTransaction removeFt = getChildFragmentManager().beginTransaction(); - removeFt.remove(currentFragment); // 或 remove + releaseResources() - removeFt.commitNowAllowingStateLoss(); - } + // 1️⃣ 如果新 Fragment 没添加过,先 add + if (!newFragment.isAdded()) { + ft.add(containerId, newFragment, newFragment.getClass().getSimpleName()); + } - // 5️⃣ 更新 currentFragment - currentFragment = newFragment; - isReplacing = false; + // 2️⃣ 显示新 Fragment + ft.show(newFragment); + + // 3️⃣ 隐藏旧 Fragment + if (currentFragment != null && currentFragment != newFragment) { + ft.hide(currentFragment); + } + + ft.commitAllowingStateLoss(); // 使用异步提交,避免阻塞 UI + + // 4️⃣ 更新引用 + currentFragment = newFragment; + isReplacing = false; + }); } + // RoomFragment.java @@ -491,31 +506,39 @@ public class RoomFragment extends BaseMvpFragment { - 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); - } - }); + View container = getView().findViewById(containerId); + if (container == null) { + isReplacing = false; + return; } + if (!isAdded()) { + isReplacing = false; + return; + } + + FragmentTransaction ft = getChildFragmentManager().beginTransaction(); + + // 1️⃣ 如果新 Fragment 没添加过,先 add + if (!newFragment.isAdded()) { + ft.add(containerId, newFragment, newFragment.getClass().getSimpleName()); + } + + // 2️⃣ 显示新 Fragment + ft.show(newFragment); + + // 3️⃣ 隐藏旧 Fragment + if (currentFragment != null && currentFragment != newFragment) { + ft.hide(currentFragment); + } + + ft.commitAllowingStateLoss(); // 使用异步提交,避免阻塞 UI + + // 4️⃣ 更新引用 + currentFragment = newFragment; + isReplacing = false; } @@ -750,7 +773,7 @@ public class RoomFragment extends BaseMvpFragment