Skip to content

Commit ef4863a

Browse files
committed
Merge pull request #109764 from ydeltastar/android-fragment-fix
Android: Ensure proper cleanup of the fragment
2 parents 781a374 + 6a3d37e commit ef4863a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

platform/android/java/lib/src/org/godotengine/godot/GodotActivity.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,16 @@ abstract class GodotActivity : FragmentActivity(), GodotHost {
9898
} else {
9999
Log.v(TAG, "Creating new Godot fragment instance.")
100100
godotFragment = initGodotInstance()
101-
supportFragmentManager.beginTransaction().replace(R.id.godot_fragment_container, godotFragment!!).setPrimaryNavigationFragment(godotFragment).commitNowAllowingStateLoss()
101+
102+
val transaction = supportFragmentManager.beginTransaction()
103+
if (currentFragment != null) {
104+
Log.v(TAG, "Removing existing fragment before replacement.")
105+
transaction.remove(currentFragment)
106+
}
107+
108+
transaction.replace(R.id.godot_fragment_container, godotFragment!!)
109+
.setPrimaryNavigationFragment(godotFragment)
110+
.commitNowAllowingStateLoss()
102111
}
103112
}
104113

platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public void onAttach(@NonNull Context context) {
126126

127127
@Override
128128
public void onDetach() {
129+
if (godotContainerLayout != null && godotContainerLayout.getParent() != null) {
130+
Log.d(TAG, "Cleaning up Godot container layout during detach.");
131+
((ViewGroup)godotContainerLayout.getParent()).removeView(godotContainerLayout);
132+
}
133+
129134
super.onDetach();
130135
parentHost = null;
131136
}
@@ -233,11 +238,21 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
233238
return downloadingExpansionView;
234239
}
235240

241+
if (godotContainerLayout != null && godotContainerLayout.getParent() != null) {
242+
Log.w(TAG, "Godot container layout already has a parent, removing it.");
243+
((ViewGroup)godotContainerLayout.getParent()).removeView(godotContainerLayout);
244+
}
245+
236246
return godotContainerLayout;
237247
}
238248

239249
@Override
240250
public void onDestroy() {
251+
if (godotContainerLayout != null && godotContainerLayout.getParent() != null) {
252+
Log.w(TAG, "Removing Godot container layout from parent during destruction.");
253+
((ViewGroup)godotContainerLayout.getParent()).removeView(godotContainerLayout);
254+
}
255+
241256
godot.onDestroy(this);
242257
super.onDestroy();
243258
}

0 commit comments

Comments
 (0)