Skip to content

Commit

Permalink
fix: add not null checks to prevent running on destroyed activity (#1148
Browse files Browse the repository at this point in the history
)

* (android) #1002: Add Null Pointer Checks to prevent Cordova from running on a destroyed activity

* (android) Add logging statements if Cordova Activity does not exist anymore (i.e. is destroyed)

Co-authored-by: Habets Rick <rick.habets@kbc.be>
  • Loading branch information
secondVISION and rick-habets-kbc-be authored Mar 27, 2021
1 parent 9dcf3eb commit 19a5feb
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions framework/src/org/apache/cordova/CordovaWebViewImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,28 @@ public void run() {
e.printStackTrace();
}

// If timeout, then stop loading and handle error
if (loadUrlTimeout == currentLoadUrlTimeout) {
// If timeout, then stop loading and handle error (if activity still exists)
if (loadUrlTimeout == currentLoadUrlTimeout && cordova.getActivity() != null) {
cordova.getActivity().runOnUiThread(loadError);
} else if (cordova.getActivity() == null) {
LOG.d(TAG, "Cordova activity does not exist.");
}
}
};

final boolean _recreatePlugins = recreatePlugins;
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
if (loadUrlTimeoutValue > 0) {
cordova.getThreadPool().execute(timeoutCheck);
if (cordova.getActivity() != null) {
final boolean _recreatePlugins = recreatePlugins;
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
if (loadUrlTimeoutValue > 0) {
cordova.getThreadPool().execute(timeoutCheck);
}
engine.loadUrl(url, _recreatePlugins);
}
engine.loadUrl(url, _recreatePlugins);
}
});
});
} else {
LOG.d(TAG, "Cordova activity does not exist.");
}
}


Expand Down Expand Up @@ -238,7 +244,11 @@ public void showWebPage(String url, boolean openExternal, boolean clearHistory,
} else {
intent.setData(uri);
}
cordova.getActivity().startActivity(intent);
if (cordova.getActivity() != null) {
cordova.getActivity().startActivity(intent);
} else {
LOG.d(TAG, "Cordova activity does not exist.");
}
} catch (android.content.ActivityNotFoundException e) {
LOG.e(TAG, "Error loading url " + url, e);
}
Expand Down Expand Up @@ -553,11 +563,15 @@ public void onPageFinishedLoading(String url) {
public void run() {
try {
Thread.sleep(2000);
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
pluginManager.postMessage("spinner", "stop");
}
});
if (cordova.getActivity() != null) {
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
pluginManager.postMessage("spinner", "stop");
}
});
} else {
LOG.d(TAG, "Cordova activity does not exist.");
}
} catch (InterruptedException e) {
}
}
Expand Down

0 comments on commit 19a5feb

Please sign in to comment.