Skip to content

Commit 70c1380

Browse files
committed
feat: change logic for nested stacks
1 parent dee9fd4 commit 70c1380

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

android/src/main/java/com/swmansion/rnscreens/ScreenFragment.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ public Screen getScreen() {
6868

6969
public void onContainerUpdate() {
7070
ScreenStackHeaderConfig config = findHeaderConfig();
71-
if (config != null && config.getScreenFragment().getActivity() != null) {
71+
boolean configInChild = checkIfChildWithConfig(getScreen());
72+
if (!configInChild && config != null && config.getScreenFragment().getActivity() != null) {
73+
// if there is no child with config, we look for a parent with config to set the orientation
7274
config.getScreenFragment().getActivity().setRequestedOrientation(config.getScreenOrientation());
7375
}
7476
}
7577

7678
private ScreenStackHeaderConfig findHeaderConfig() {
77-
ViewParent parent = getScreen();
79+
ViewParent parent = getScreen().getContainer();
7880
while (parent != null) {
7981
if (parent instanceof Screen) {
8082
for (int i = 0; i < ((Screen) parent).getChildCount(); i++) {
@@ -89,6 +91,22 @@ private ScreenStackHeaderConfig findHeaderConfig() {
8991
return null;
9092
}
9193

94+
protected static boolean checkIfChildWithConfig(View view) {
95+
if (view instanceof ViewGroup) {
96+
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
97+
View child = ((ViewGroup) view).getChildAt(i);
98+
if (child instanceof ScreenStackHeaderConfig) {
99+
return true;
100+
} else {
101+
if (checkIfChildWithConfig(child)) {
102+
return true;
103+
}
104+
}
105+
}
106+
}
107+
return false;
108+
}
109+
92110
protected void dispatchOnWillAppear() {
93111
((ReactContext) mScreenView.getContext())
94112
.getNativeModule(UIManagerModule.class)

android/src/main/java/com/swmansion/rnscreens/ScreenStackHeaderConfig.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ public void onUpdate() {
172172
}
173173

174174
// orientation
175-
activity.setRequestedOrientation(mScreenOrientation);
175+
if (!ScreenFragment.checkIfChildWithConfig(getScreen() != null ? getScreen().getChildAt(1) : null)) {
176+
// we check if there is no child that provides config, since then we shouldn't change orientation here (child with index 0 is this header config)
177+
activity.setRequestedOrientation(mScreenOrientation);
178+
}
176179

177180
if (mIsHidden) {
178181
if (mToolbar.getParent() != null) {

0 commit comments

Comments
 (0)