Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: android onDropViewInstance not invoked when page popped #7919

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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