Skip to content

Commit

Permalink
fix: android onDropViewInstance not invoked when page popped
Browse files Browse the repository at this point in the history
  • Loading branch information
NormanWangEndeavor committed Oct 8, 2024
1 parent 8c64969 commit 010b7f1
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -64,7 +69,29 @@ public ReactView asView() {

@Override
public void destroy() {
// get current children and id
ViewGroup rootViewGroup = getRootViewGroup();
int childCount = rootViewGroup.getChildCount();
int id = rootViewGroup.getId();
List<View> children = new ArrayList<>();
for (int i = 0; i < childCount; i++) {
children.add(rootViewGroup.getChildAt(i));
}

// since RN 0.71.x unmount will remove all children and reset the id
// which cause the onDropViewInstance not called.
unmountReactApplication();

// restore removed children and revert the id since RN 0.71.x
// then the onDropViewInstance will be called.
// and NativeViewHierarchyManager will help to remove these children later.
for (int i = 0; i < children.size(); i++) {
View child = children.get(i);
if (rootViewGroup.indexOfChild(child) < 0) {
rootViewGroup.addView(child);
}
}
rootViewGroup.setId(id);
}

public void sendComponentWillStart(ComponentType type) {
Expand Down

0 comments on commit 010b7f1

Please sign in to comment.