From b1551cad9816d40fa5e9430d80521c0650b3bc7e Mon Sep 17 00:00:00 2001 From: Rich Turner Date: Fri, 23 Mar 2018 20:19:52 +0000 Subject: [PATCH] CB-13975: (android) Fix to fire pause event when cdvStartInBackground=true --- .../src/org/apache/cordova/CoreAndroid.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/framework/src/org/apache/cordova/CoreAndroid.java b/framework/src/org/apache/cordova/CoreAndroid.java index e384f8d7..46a23edf 100755 --- a/framework/src/org/apache/cordova/CoreAndroid.java +++ b/framework/src/org/apache/cordova/CoreAndroid.java @@ -43,6 +43,7 @@ public class CoreAndroid extends CordovaPlugin { private BroadcastReceiver telephonyReceiver; private CallbackContext messageChannel; private PluginResult pendingResume; + private PluginResult pendingPause; private final Object messageChannelLock = new Object(); /** @@ -113,6 +114,10 @@ public class CoreAndroid extends CordovaPlugin { else if (action.equals("messageChannel")) { synchronized(messageChannelLock) { messageChannel = callbackContext; + if (pendingPause != null) { + sendEventMessage(pendingPause); + pendingPause = null; + } if (pendingResume != null) { sendEventMessage(pendingResume); pendingResume = null; @@ -321,7 +326,19 @@ public class CoreAndroid extends CordovaPlugin { } catch (JSONException e) { LOG.e(TAG, "Failed to create event message", e); } - sendEventMessage(new PluginResult(PluginResult.Status.OK, obj)); + PluginResult result = new PluginResult(PluginResult.Status.OK, obj); + + if (messageChannel == null) { + LOG.i(TAG, "Request to send event before messageChannel initialised: " + action); + if ("pause".equals(action)) { + pendingPause = result; + } else if ("resume".equals(action)) { + // When starting normally onPause then onResume is called + pendingPause = null; + } + } else { + sendEventMessage(result); + } } private void sendEventMessage(PluginResult payload) {