My app is a Cordova app which uses the barcode scanner plugin.
I am on MacOS, Cordova Android 6.1.2, barcode-scanner-plugin 6.0.6.
Some users complained that there is a problem when they use the app :
- the app starts
- they click on an img which launches the barcode scanner plugin
- on the barcode activity, they catch a QR Code. It's OK so far...
- then, the app tries to come back to the first screen and crashes
For those people, the problem is that Android, because of low memory detection, decided to destroy the Cordova activity.
Knowing that, I decided to use onPause/onResume Cordova events to handle this case.
But I face a problem, the onResume is not always called. When the Cordova activity is not destroyed, the onResume callback is well called. But when the Cordova activity is really destroyed (simulating it in configuring the "Don't keep activities" preference), then the onResume callback is not called!
My code :
$(document).ready(function() {
if (console) {
console.log("--> évènement document.ready (JQuery) lancé");
}
document.addEventListener("deviceready", function() {
if (console) {
console.log("--> évènement deviceready (Cordova) lancé");
}
document.addEventListener('pause', impots.scan.onPause, false);
document.addEventListener('resume', impots.scan.onResume, false);
}, false);
});
impots.scan.onPause = function() {
console.log('Evenement pause ----------------');
impots.scan.debugAppState()
}
impots.scan.onResume = function(event) {
console.log('Evenement resume ----------------');
console.log('event ' + JSON.stringify(event));
var storedState = window.localStorage.getItem(impots.scan.APP_STORAGE_KEY);
// si storedState, c'est qu'Android a supprimé la Cordova Activity et il faut reconstituer l'UI
if (storedState && storedState.SPI) {
console.log('Chargement appStage a partir du LocalStorage')
impots.scan.updateModel(storedState.SPI)
}
if (event.pendingResult) {
if (event.pendingResult.pluginStatus === 'OK') {
console.log('Recuperation d\'un QR code suite à la suppression de la Cordova activity')
app.updateModel(event.pendingResult.result.text)
} else {
console.log('Probleme de réupération QR suite a la suppression de la Cordova activity')
}
}
impots.scan.updateUI()
}
Log when the Cordova activity is not destroyed :
15:15:07.156 30876 30876 I CordovaLog: Changing log level to VERBOSE(2)
15:15:07.157 30876 30876 I CordovaActivity: Apache Cordova native platform version 6.1.2 is starting
15:15:07.157 30876 30876 D CordovaActivity: CordovaActivity.onCreate()
15:15:07.267 30876 30876 I chromium: [INFO:library_loader_hooks.cc(140)] Chromium logging enabled: level = 0, default verbosity = 0
15:15:07.456 30876 30903 W cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 30876
15:15:07.516 30876 30876 D SystemWebViewEngine: CordovaWebView is running on device made by: asus
15:15:07.575 30876 30876 D CordovaActivity: Started the activity.
15:15:07.575 30876 30876 D CordovaActivity: Resumed the activity.
15:15:07.938 30876 30876 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 30876
15:15:08.915 30876 30876 I chromium: [INFO:CONSOLE(12)] "mobileinit event triggered, initialization...", source: file:///android_asset/www/impots/js/impots-override.js (12)
15:15:11.061 30876 30876 I chromium: [INFO:CONSOLE(49)] "--> évènement document.ready (JQuery) lancé", source: file:///android_asset/www/impots/js/impots.js (49)
15:15:11.575 30876 30876 I chromium: [INFO:CONSOLE(70)] "--> évènement deviceready (Cordova) lancé", source: file:///android_asset/www/impots/js/impots.js (70)
15:15:19.319 30876 30876 D CordovaActivity: Paused the activity.
15:15:19.399 30876 30876 W cr_Ime : updateState: type [0->0], flags [0], show [false],
15:15:19.403 30876 30876 I chromium: [INFO:CONSOLE(256)] "Evenement pause ----------------", source: file:///android_asset/www/impots/js/impots-scan.js (256)
15:15:19.403 30876 30876 I chromium: [INFO:CONSOLE(230)] "appState {}", source: file:///android_asset/www/impots/js/impots-scan.js (230)
15:15:19.404 30876 30876 I chromium: [INFO:CONSOLE(234)] "Stockage de l'appState en LocalStorage", source: file:///android_asset/www/impots/js/impots-scan.js (234)
15:15:19.565 187 187 W SurfaceFlinger: couldn't log to binary event log: overflow.
15:15:19.655 616 2208 W ActivityManager: Unable to start service Intent { cmp=com.google.android.apps.photos/.cameraassistant.CameraAssistantService (has extras) } U=0: not found
15:15:19.835 202 208 E ANDR-PERF-LOCK: Failed to apply optimization for resource: 4 level: 0
15:15:19.838 194 652 W Camera2-ZslProcessor3: ZslProcessor3: Unable to find the android.request.pipelineMaxDepth, use default pipeline max depth 4
15:15:20.737 30876 30876 D CordovaActivity: Stopped the activity.
15:15:22.381 616 632 W ActivityManager: Activity pause timeout for ActivityRecord{fa630ef u0 fr.gouv.finances.smartphone.android/com.google.zxing.client.android.CaptureActivity t250 f}
15:15:23.077 202 208 E ANDR-PERF-LOCK: Failed to reset optimization for resource: 4 level: 0
15:15:23.101 30876 30876 D CordovaActivity: Incoming Result. Request code = 195543262
15:15:23.102 30876 30876 D CordovaInterfaceImpl: Sending activity result to plugin
15:15:23.102 30876 30876 D BarcodeScanner: dans onActivityResult
15:15:23.104 30876 30876 D CordovaActivity: Started the activity.
15:15:23.104 30876 30876 D CordovaActivity: Resumed the activity.
15:15:23.251 30876 30876 I chromium: [INFO:CONSOLE(266)] "Evenement resume ----------------", source: file:///android_asset/www/impots/js/impots-scan.js (266)
15:15:23.251 30876 30876 I chromium: [INFO:CONSOLE(267)] "event {"isTrusted":false,"action":"resume"}", source: file:///android_asset/www/impots/js/impots-scan.js (267)
The resume event is well called.
When I use the "Don't keep activities" on the device and retry:
15:20:43.396 1144 1355 W OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
15:20:43.396 1144 1355 W OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
15:20:44.800 1672 1672 W System : ClassLoader referenced unknown path: /data/app/fr.gouv.finances.smartphone.android-1/lib/arm
15:20:44.830 1672 1672 I CordovaLog: Changing log level to VERBOSE(2)
15:20:44.830 1672 1672 I CordovaActivity: Apache Cordova native platform version 6.1.2 is starting
15:20:44.830 1672 1672 D CordovaActivity: CordovaActivity.onCreate()
15:20:44.944 1672 1672 I chromium: [INFO:library_loader_hooks.cc(140)] Chromium logging enabled: level = 0, default verbosity = 0
15:20:45.160 1672 1703 W cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 1672
15:20:45.221 1672 1672 D SystemWebViewEngine: CordovaWebView is running on device made by: asus
15:20:45.270 1672 1672 D CordovaActivity: Started the activity.
15:20:45.270 1672 1672 D CordovaActivity: Resumed the activity.
15:20:45.640 1672 1672 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 1672
15:20:46.605 1672 1672 I chromium: [INFO:CONSOLE(12)] "mobileinit event triggered, initialization...", source: file:///android_asset/www/impots/js/impots-override.js (12)
15:20:48.569 1672 1672 I chromium: [INFO:CONSOLE(49)] "--> évènement document.ready (JQuery) lancé", source: file:///android_asset/www/impots/js/impots.js (49)
15:20:49.026 1672 1672 I chromium: [INFO:CONSOLE(70)] "--> évènement deviceready (Cordova) lancé", source: file:///android_asset/www/impots/js/impots.js (70)
15:21:07.859 1672 1672 D CordovaActivity: Paused the activity.
15:21:07.929 1672 1672 W cr_Ime : updateState: type [0->0], flags [0], show [false],
15:21:07.940 1672 1672 I chromium: [INFO:CONSOLE(256)] "Evenement pause ----------------", source: file:///android_asset/www/impots/js/impots-scan.js (256)
15:21:07.943 1672 1672 I chromium: [INFO:CONSOLE(230)] "appState {}", source: file:///android_asset/www/impots/js/impots-scan.js (230)
15:21:07.943 1672 1672 I chromium: [INFO:CONSOLE(234)] "Stockage de l'appState en LocalStorage", source: file:///android_asset/www/impots/js/impots-scan.js (234)
15:21:08.103 187 187 W SurfaceFlinger: couldn't log to binary event log: overflow.
15:21:08.206 616 2094 W ActivityManager: Unable to start service Intent { cmp=com.google.android.apps.photos/.cameraassistant.CameraAssistantService (has extras) } U=0: not found
15:21:08.395 202 208 E ANDR-PERF-LOCK: Failed to apply optimization for resource: 4 level: 0
15:21:08.400 194 1102 W Camera2-ZslProcessor3: ZslProcessor3: Unable to find the android.request.pipelineMaxDepth, use default pipeline max depth 4
15:21:09.394 1672 1672 D CordovaActivity: Stopped the activity.
15:21:09.421 1672 1672 D CordovaActivity: CordovaActivity.onDestroy()
15:21:09.423 1672 1672 W cr_AwContents: WebView.destroy() called while WebView is still attached to window.
15:21:09.466 1672 1672 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 1672
15:21:09.468 1672 1672 W cr_AwContents: Application attempted to call on a destroyed WebView
15:21:09.468 1672 1672 W cr_AwContents: java.lang.Throwable
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.android_webview.AwContents.isDestroyed(AwContents.java:1265)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.android_webview.AwContents.isDestroyedOrNoOperation(AwContents.java:1195)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.android_webview.AwContents.getTitle(AwContents.java:2063)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.android_webview.AwWebContentsDelegateAdapter.loadingStateChanged(AwWebContentsDelegateAdapter.java:280)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
15:21:09.468 1672 1672 W cr_AwContents: at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:41)
15:21:09.468 1672 1672 W cr_AwContents: at android.os.Handler.dispatchMessage(Handler.java:102)
15:21:09.468 1672 1672 W cr_AwContents: at android.os.Looper.loop(Looper.java:148)
15:21:09.468 1672 1672 W cr_AwContents: at android.app.ActivityThread.main(ActivityThread.java:5417)
15:21:09.468 1672 1672 W cr_AwContents: at java.lang.reflect.Method.invoke(Native Method)
15:21:09.468 1672 1672 W cr_AwContents: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
15:21:09.468 1672 1672 W cr_AwContents: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
15:21:09.469 1672 1703 W cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 1672
15:21:16.298 616 632 W ActivityManager: Activity pause timeout for ActivityRecord{4e505a3 u0 fr.gouv.finances.smartphone.android/com.google.zxing.client.android.CaptureActivity t251 f}
15:21:16.747 202 208 E ANDR-PERF-LOCK: Failed to reset optimization for resource: 4 level: 0
15:21:16.774 1672 1672 I CordovaLog: Changing log level to VERBOSE(2)
15:21:16.774 1672 1672 I CordovaActivity: Apache Cordova native platform version 6.1.2 is starting
15:21:16.774 1672 1672 D CordovaActivity: CordovaActivity.onCreate()
15:21:16.781 1672 1703 W cr_BindingManager: Cannot setInForeground() - never saw a connection for the pid: 1672
15:21:16.799 1672 1672 D SystemWebViewEngine: CordovaWebView is running on device made by: asus
15:21:16.836 1672 1672 D CordovaActivity: Started the activity.
15:21:16.838 1672 1672 D CordovaActivity: Incoming Result. Request code = 195543262
15:21:16.839 1672 1672 D BarcodeScanner: dans onRestoreStateForActivityResult
15:21:16.839 1672 1672 D CordovaInterfaceImpl: Sending activity result to plugin
15:21:16.839 1672 1672 D BarcodeScanner: dans onActivityResult
15:21:16.839 1672 1672 D CordovaActivity: Resumed the activity.
15:21:17.147 1672 1672 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 1672
15:21:17.497 1672 1672 I chromium: [INFO:CONSOLE(12)] "mobileinit event triggered, initialization...", source: file:///android_asset/www/impots/js/impots-override.js (12)
15:21:17.954 1672 1672 I chromium: [INFO:CONSOLE(49)] "--> évènement document.ready (JQuery) lancé", source: file:///android_asset/www/impots/js/impots.js (49)
15:21:18.311 1672 1672 I chromium: [INFO:CONSOLE(70)] "--> évènement deviceready (Cordova) lancé", source: file:///android_asset/www/impots/js/impots.js (70)
This time the onResume is not called. However, I assume a result was found by the plugin because the log tells "CordovaActivity: Incoming Result. Request code = 195543262" and "CordovaInterfaceImpl: Sending activity result to plugin"
I see a problem though : "Application attempted to call on a destroyed WebView". It's just a warning but it can be the reason why the onResume is not called, I really don't figure out.
My CordovaActivity is like that :
public class MainActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
loadUrl(launchUrl);
}
@Override
public void onDestroy()
{
super.onDestroy();
}
}
Any idea would be appreciated!