From 8c1cae6aa97daf3a36bee8dcb740a799d32359a0 Mon Sep 17 00:00:00 2001 From: Norman Wang Date: Mon, 30 Sep 2024 10:45:54 +0800 Subject: [PATCH] fix: android onDropViewInstance not invoked when page popped --- .../react/ReactView.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/react/ReactView.java b/lib/android/app/src/main/java/com/reactnativenavigation/react/ReactView.java index 90032dfd55d..70b4fa18014 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/react/ReactView.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/react/ReactView.java @@ -4,6 +4,8 @@ import android.content.Context; import android.os.Bundle; import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactRootView; @@ -20,6 +22,9 @@ import androidx.annotation.RestrictTo; +import java.util.ArrayList; +import java.util.List; + @SuppressLint("ViewConstructor") public class ReactView extends ReactRootView implements IReactView, Renderable { @@ -64,7 +69,26 @@ public ReactView asView() { @Override public void destroy() { + // get current children and id + ViewGroup rootViewGroup = getRootViewGroup(); + int childCount = rootViewGroup.getChildCount(); + int id = rootViewGroup.getId(); + List children = new ArrayList<>(); + for (int i = 0; i < childCount; i++) { + children.add(rootViewGroup.getChildAt(i)); + } + + // unmount will remove all children and reset the id + // which cause the onDropViewInstance not called. unmountReactApplication(); + + // restore removed children and revert the id + // then the onDropViewInstance will be called. + // and NativeViewHierarchyManager will help to remove these children later. + for (int i = 0; i < children.size(); i++) { + rootViewGroup.addView(children.get(i)); + } + rootViewGroup.setId(id); } public void sendComponentWillStart(ComponentType type) {