Skip to content Skip to sidebar Skip to footer

App Crashes On Cordova Alert

On some Android devices, my Cordova-based app crashes around the time I prompt the user if they'd like to receive notifications, which happens on the first app startup. Here's a ty

Solution 1:

This is because you are trying to display alert dialog while the activity is finishing.

You can subclass CordovaChromeClient and check for activity.isFinishing() on onJsAlert()

I have created a project on Github that solves this bug: https://github.com/kruyvanna/CordovaAlertBug_Android

You can see an example below.

classCordovaOnJsAlertBugextendsDroidGap{
@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/index.html");
}

@Overridepublicvoidinit() {
    Log.e(TAG, "init()");
    CordovaWebViewwebView=newCordovaWebView(CordovaOnJsAlertBug.this);
    CordovaWebViewClient webViewClient;
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
    {
        webViewClient = newCordovaWebViewClient(this, webView);
    }
    else
    {
        webViewClient = newIceCreamCordovaWebViewClient(this, webView);
    }
    this.init(webView, webViewClient, newMyCordovaChromeClient(this, webView));
}

privateclassMyCordovaChromeClientextendsCordovaChromeClient{
    private CordovaInterface cordova;
    publicMyCordovaChromeClient(CordovaInterface ctx, CordovaWebView app) {
        super(ctx, app);
        this.cordova = ctx;
    }


    @OverridepublicbooleanonJsAlert(WebView view, String url, String message, JsResult result) {
        if(cordova.getActivity().isFinishing()){
            Log.w(TAG, "Trying to alert while activity is finishing!! -> ignore");
            result.cancel();
            returntrue;
        }

        returnsuper.onJsAlert(view, url, message, result);
    }
}
}

Post a Comment for "App Crashes On Cordova Alert"