Skip to content

Commit c741acf

Browse files
authored
Unwrap Context in order to retrieve Activity subclass (#59)
This fixes crash on Expo client which is wrapping Activity prior to passing it as a context to the root view. After my recent change in the logic on how we access main activity we know extract the reference to it using `getContext` from the root view. Previously we were using `getTopLevelActivity` which wasn't working well in the cases where other non-react-native activities were transitioning in or out. The new approach however turned out not to be the best as for example expo client does not pass activity instance as a context directly to the root view. Instead the activity class is wrapped in ContextThemeWrapper ([see it here](https://github.com/expo/expo/blame/41458d1de91544c41097818db21e44e9aa84688c/android/expoview/src/main/java/versioned/host/exp/exponent/ReactUnthemedRootView.java#L13)). We now try to unwrap the context if it is not a fragment activity using `getBaseContext` This fixes expo/expo#3191
1 parent 11906bf commit c741acf

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.content.Context;
5+
import android.content.ContextWrapper;
56
import android.support.annotation.Nullable;
67
import android.support.v4.app.Fragment;
78
import android.support.v4.app.FragmentActivity;
@@ -93,8 +94,12 @@ private FragmentActivity findRootFragmentActivity() {
9394
if (!(parent instanceof ReactRootView)) {
9495
throw new IllegalStateException("ScreenContainer is not attached under ReactRootView");
9596
}
96-
// ReactRootView is expected to be initialized with the main React Activity as a context
97+
// ReactRootView is expected to be initialized with the main React Activity as a context but
98+
// in case of Expo the activity is wrapped in ContextWrapper and we need to unwrap it
9799
Context context = ((ReactRootView) parent).getContext();
100+
while (!(context instanceof FragmentActivity) && context instanceof ContextWrapper) {
101+
context = ((ContextWrapper) context).getBaseContext();
102+
}
98103
if (!(context instanceof FragmentActivity)) {
99104
throw new IllegalStateException(
100105
"In order to use RNScreens components your app's activity need to extend ReactFragmentActivity or ReactCompatActivity");

0 commit comments

Comments
 (0)