Skip to content

Commit b74e7f1

Browse files
authored
Merge pull request #172 from googlemaps/fix/fix-android-sendcommand-crash-issues
fix: fixes crash issues on android if fragment disposed while sending events
2 parents 467f0c2 + 0579276 commit b74e7f1

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

android/src/main/java/com/google/android/react/navsdk/NavViewManager.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)