Skip to content

Commit 8be6668

Browse files
fix crashes on android by adding null safety (#8267)
* try to fix some android crashes * try to fix some android crashes --------- Co-authored-by: mark.dev <50657916+markdevocht@users.noreply.github.com>
1 parent 3c42afb commit 8be6668

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

android/src/main/java/com/reactnativenavigation/hierarchy/root/RootAnimator.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController
1010
open class RootAnimator {
1111

1212
open fun setRoot(appearing: ViewController<*>, disappearing: ViewController<*>?, setRoot: TransitionAnimationOptions, onAnimationEnd: ()->Unit) {
13+
if (appearing.isDestroyed) {
14+
onAnimationEnd()
15+
return
16+
}
1317
appearing.view.visibility = View.VISIBLE
1418

1519
if (!setRoot.hasValue() || (!setRoot.enter.hasAnimation() && !setRoot.exit.hasAnimation())) {
@@ -22,10 +26,9 @@ open class RootAnimator {
2226
val appearingAnimation = if (setRoot.enter.hasAnimation()) {
2327
setRoot.enter.getAnimation(appearing.view)
2428
} else null
25-
val disappearingAnimation = if (disappearing != null && setRoot.exit.hasAnimation()) {
29+
val disappearingAnimation = if (disappearing != null && !disappearing.isDestroyed && setRoot.exit.hasAnimation()) {
2630
setRoot.exit.getAnimation(disappearing.view)
2731
} else null
28-
2932
when {
3033
appearingAnimation != null && disappearingAnimation != null -> animationSet.playTogether(appearingAnimation, disappearingAnimation)
3134
appearingAnimation != null -> animationSet.play(appearingAnimation)

android/src/main/java/com/reactnativenavigation/react/NavigationTurboModule.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ class NavigationTurboModule(
8484
handle {
8585
Log.d("NavigationTurboModule", "setRoot handle ${Thread.currentThread()}")
8686
val viewController = layoutFactory.create(layoutTree)
87+
val activity = currentActivity
88+
if (activity == null) {
89+
promise.reject("ACTIVITY_NULL", "Activity is null")
90+
return@handle
91+
}
8792
navigator()?.setRoot(
8893
viewController,
8994
NativeCommandListener("setRoot", commandId, promise, eventEmitter, now)

android/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.reactnativenavigation.viewcontrollers.viewcontroller.Presenter;
88
import com.reactnativenavigation.react.events.EventEmitter;
99
import com.reactnativenavigation.viewcontrollers.child.ChildControllersRegistry;
10+
import com.reactnativenavigation.NavigationApplication;
1011
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController;
1112
import com.reactnativenavigation.viewcontrollers.stack.topbar.TopBarController;
1213

@@ -32,7 +33,7 @@ public StackControllerBuilder(Activity activity, EventEmitter eventEmitter) {
3233
this.activity = activity;
3334
this.eventEmitter = eventEmitter;
3435
presenter = new Presenter(activity, new Options());
35-
animator = new StackAnimator(activity);
36+
animator = new StackAnimator(activity != null ? activity : NavigationApplication.instance);
3637
}
3738

3839
public StackControllerBuilder setEventEmitter(EventEmitter eventEmitter) {

android/src/main/java/com/reactnativenavigation/viewcontrollers/viewcontroller/Presenter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void onViewBroughtToFront(ViewController<?> viewController, Options optio
6565
}
6666

6767
private void applyOrientation(OrientationOptions options) {
68-
activity.setRequestedOrientation(options.getValue());
68+
if (activity != null) activity.setRequestedOrientation(options.getValue());
6969
}
7070

7171
private void applyViewOptions(ViewController view, Options options) {
@@ -127,6 +127,7 @@ private void mergeNavigationBarVisibility(NavigationBarOptions options) {
127127
}
128128

129129
private void applyNavigationBarVisibility(NavigationBarOptions options) {
130+
if (activity == null) return;
130131
View decorView = activity.getWindow().getDecorView();
131132
if (options.isVisible.isTrueOrUndefined()) {
132133
SystemUiUtils.showNavigationBar(activity.getWindow(), decorView);

android/src/main/java/com/reactnativenavigation/viewcontrollers/viewcontroller/RootPresenter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public RootPresenter(RootAnimator animator, LayoutDirectionApplier layoutDirecti
3636

3737
public void setRoot(ViewController appearingRoot, ViewController<?> disappearingRoot, Options defaultOptions, CommandListener listener) {
3838
layoutDirectionApplier.apply(appearingRoot, defaultOptions);
39+
if (appearingRoot.isDestroyed()) {
40+
listener.onError("Could not set root - appearingRoot is already destroyed");
41+
return;
42+
}
3943
rootLayout.addView(appearingRoot.getView(), matchParentWithBehaviour(new BehaviourDelegate(appearingRoot)));
4044
Options options = appearingRoot.resolveCurrentOptions(defaultOptions);
4145
AnimationOptions enter = options.animations.setRoot.getEnter();
@@ -60,6 +64,10 @@ private void animateSetRootAndReportSuccess(ViewController root,
6064
CommandListener listener,
6165
Options options)
6266
{
67+
if (root.isDestroyed()) {
68+
listener.onError("Could not set root - root is already destroyed");
69+
return;
70+
}
6371
AnimationOptions exit = options.animations.setRoot.getExit();
6472
AnimationOptions enter = options.animations.setRoot.getEnter();
6573
if ((enter.enabled.isTrueOrUndefined() && enter.hasAnimation())
@@ -68,7 +76,9 @@ private void animateSetRootAndReportSuccess(ViewController root,
6876
disappearingRoot,
6977
options.animations.setRoot,
7078
() -> {
71-
listener.onSuccess(root.getId());
79+
if (!root.isDestroyed()) {
80+
listener.onSuccess(root.getId());
81+
}
7282
return Unit.INSTANCE;
7383
});
7484
} else {

android/src/main/java/com/reactnativenavigation/viewcontrollers/viewcontroller/ViewController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public void performOnParentStack(Func1<StackController> task) {
235235
public T getView() {
236236
if (view == null) {
237237
if (isDestroyed) {
238-
throw new RuntimeException("Tried to create view after it has already been destroyed");
238+
throw new RuntimeException("Tried to create view for " + getClass().getSimpleName() + " (id: " + id + ") after it has already been destroyed");
239239
}
240240
view = createView();
241241
view.setOnHierarchyChangeListener(this);

0 commit comments

Comments
 (0)