Skip to content

Commit

Permalink
Merge pull request #172 from googlemaps/fix/fix-android-sendcommand-c…
Browse files Browse the repository at this point in the history
…rash-issues

fix: fixes crash issues on android if fragment disposed while sending events
  • Loading branch information
caio1985 authored Jun 7, 2024
2 parents 467f0c2 + 0579276 commit b74e7f1
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public void manuallyLayoutChildren(View view) {
}

private void sendCommandToReactNative(String functionName, Object args) {
if (navViewFragment.requireActivity() != null && reactContext != null) {
if (hasValidFragment()) {
CatalystInstance catalystInstance = reactContext.getCatalystInstance();
WritableNativeArray params = new WritableNativeArray();
if (args != null) {
Expand All @@ -492,7 +492,7 @@ private void sendCommandToReactNative(String functionName, Object args) {

@Override
public void onArrival(ArrivalEvent event) {
if (navViewFragment.requireActivity() != null && reactContext != null) {
if (hasValidFragment()) {
CatalystInstance catalystInstance = reactContext.getCatalystInstance();

WritableMap map = Arguments.createMap();
Expand Down Expand Up @@ -608,7 +608,7 @@ public String getNavSDKVersion() {

@Override
public void onTurnByTurn(NavInfo navInfo) {
if (navViewFragment.requireActivity() != null && reactContext != null) {
if (hasValidFragment()) {
CatalystInstance catalystInstance = reactContext.getCatalystInstance();

WritableMap map = Arguments.createMap();
Expand Down Expand Up @@ -714,4 +714,17 @@ public void onMarkerInfoWindowTapped(Marker marker) {
NavViewFragment getNavViewFragment() {
return navViewFragment;
}

/**
* Helper method to check if the fragment is added and the reactContext is not null.
* requireActivity throws an exception if the fragment is not added or the activity is null,
* in this case exception is caught and false is returned.
*/
private boolean hasValidFragment() {
try {
return navViewFragment.isAdded() && navViewFragment.requireActivity() != null && reactContext != null;
} catch (Exception e) {
return false;
}
}
}

0 comments on commit b74e7f1

Please sign in to comment.