@@ -480,7 +480,7 @@ public void manuallyLayoutChildren(View view) {
480480 }
481481
482482 private void sendCommandToReactNative (String functionName , Object args ) {
483- if (navViewFragment . requireActivity () != null && reactContext != null ) {
483+ if (hasValidFragment () ) {
484484 CatalystInstance catalystInstance = reactContext .getCatalystInstance ();
485485 WritableNativeArray params = new WritableNativeArray ();
486486 if (args != null ) {
@@ -492,7 +492,7 @@ private void sendCommandToReactNative(String functionName, Object args) {
492492
493493 @ Override
494494 public void onArrival (ArrivalEvent event ) {
495- if (navViewFragment . requireActivity () != null && reactContext != null ) {
495+ if (hasValidFragment () ) {
496496 CatalystInstance catalystInstance = reactContext .getCatalystInstance ();
497497
498498 WritableMap map = Arguments .createMap ();
@@ -608,7 +608,7 @@ public String getNavSDKVersion() {
608608
609609 @ Override
610610 public void onTurnByTurn (NavInfo navInfo ) {
611- if (navViewFragment . requireActivity () != null && reactContext != null ) {
611+ if (hasValidFragment () ) {
612612 CatalystInstance catalystInstance = reactContext .getCatalystInstance ();
613613
614614 WritableMap map = Arguments .createMap ();
@@ -714,4 +714,17 @@ public void onMarkerInfoWindowTapped(Marker marker) {
714714 NavViewFragment getNavViewFragment () {
715715 return navViewFragment ;
716716 }
717+
718+ /**
719+ * Helper method to check if the fragment is added and the reactContext is not null.
720+ * requireActivity throws an exception if the fragment is not added or the activity is null,
721+ * in this case exception is caught and false is returned.
722+ */
723+ private boolean hasValidFragment () {
724+ try {
725+ return navViewFragment .isAdded () && navViewFragment .requireActivity () != null && reactContext != null ;
726+ } catch (Exception e ) {
727+ return false ;
728+ }
729+ }
717730}
0 commit comments