Skip to content

Commit 8c1cae6

Browse files
fix: android onDropViewInstance not invoked when page popped
1 parent 8c64969 commit 8c1cae6

File tree

1 file changed

+24
-0
lines changed
  • lib/android/app/src/main/java/com/reactnativenavigation/react

1 file changed

+24
-0
lines changed

lib/android/app/src/main/java/com/reactnativenavigation/react/ReactView.java

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.content.Context;
55
import android.os.Bundle;
66
import android.view.MotionEvent;
7+
import android.view.View;
8+
import android.view.ViewGroup;
79

810
import com.facebook.react.ReactInstanceManager;
911
import com.facebook.react.ReactRootView;
@@ -20,6 +22,9 @@
2022

2123
import androidx.annotation.RestrictTo;
2224

25+
import java.util.ArrayList;
26+
import java.util.List;
27+
2328
@SuppressLint("ViewConstructor")
2429
public class ReactView extends ReactRootView implements IReactView, Renderable {
2530

@@ -64,7 +69,26 @@ public ReactView asView() {
6469

6570
@Override
6671
public void destroy() {
72+
// get current children and id
73+
ViewGroup rootViewGroup = getRootViewGroup();
74+
int childCount = rootViewGroup.getChildCount();
75+
int id = rootViewGroup.getId();
76+
List<View> children = new ArrayList<>();
77+
for (int i = 0; i < childCount; i++) {
78+
children.add(rootViewGroup.getChildAt(i));
79+
}
80+
81+
// unmount will remove all children and reset the id
82+
// which cause the onDropViewInstance not called.
6783
unmountReactApplication();
84+
85+
// restore removed children and revert the id
86+
// then the onDropViewInstance will be called.
87+
// and NativeViewHierarchyManager will help to remove these children later.
88+
for (int i = 0; i < children.size(); i++) {
89+
rootViewGroup.addView(children.get(i));
90+
}
91+
rootViewGroup.setId(id);
6892
}
6993

7094
public void sendComponentWillStart(ComponentType type) {

0 commit comments

Comments
 (0)