diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotActivity.kt b/platform/android/java/lib/src/org/godotengine/godot/GodotActivity.kt index 5523c50592d..8ef817ad9db 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotActivity.kt +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotActivity.kt @@ -98,7 +98,16 @@ abstract class GodotActivity : FragmentActivity(), GodotHost { } else { Log.v(TAG, "Creating new Godot fragment instance.") godotFragment = initGodotInstance() - supportFragmentManager.beginTransaction().replace(R.id.godot_fragment_container, godotFragment!!).setPrimaryNavigationFragment(godotFragment).commitNowAllowingStateLoss() + + val transaction = supportFragmentManager.beginTransaction() + if (currentFragment != null) { + Log.v(TAG, "Removing existing fragment before replacement.") + transaction.remove(currentFragment) + } + + transaction.replace(R.id.godot_fragment_container, godotFragment!!) + .setPrimaryNavigationFragment(godotFragment) + .commitNowAllowingStateLoss() } } diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java b/platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java index 4f76ba94577..42f32e93576 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java @@ -126,6 +126,11 @@ public class GodotFragment extends Fragment implements IDownloaderClient, GodotH @Override public void onDetach() { + if (godotContainerLayout != null && godotContainerLayout.getParent() != null) { + Log.d(TAG, "Cleaning up Godot container layout during detach."); + ((ViewGroup)godotContainerLayout.getParent()).removeView(godotContainerLayout); + } + super.onDetach(); parentHost = null; } @@ -233,11 +238,21 @@ public class GodotFragment extends Fragment implements IDownloaderClient, GodotH return downloadingExpansionView; } + if (godotContainerLayout != null && godotContainerLayout.getParent() != null) { + Log.w(TAG, "Godot container layout already has a parent, removing it."); + ((ViewGroup)godotContainerLayout.getParent()).removeView(godotContainerLayout); + } + return godotContainerLayout; } @Override public void onDestroy() { + if (godotContainerLayout != null && godotContainerLayout.getParent() != null) { + Log.w(TAG, "Removing Godot container layout from parent during destruction."); + ((ViewGroup)godotContainerLayout.getParent()).removeView(godotContainerLayout); + } + godot.onDestroy(this); super.onDestroy(); }