Tuesday, 21 September 2021

Handle Promise from Android WebView with evaluateJavascript

I'm working with the Android WebView and trying to handle the return of a JavaScript promise from the WebView on the Java side after calling it with evaluateJavascript.

document.java

Button buttonAnnotations = findViewById(R.id.buttonAnnotations);
buttonAnnotations.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        wv.evaluateJavascript("javascript:getAnnotations();", new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
                Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
            }
        });
    }
});

index.html

async function getAnnotations() {
    await pdfViewer.getAnnotations().then(result => {
        return JSON.stringify(result ,null,2);
    });
}

If I change the getAnnotations() function to not be async and return a string all works fine, so I'm trying to figure out how to handle this promise in the java code to get the result.

I've seen a few similar questions but none of the answers seemed to work in this case.



from Handle Promise from Android WebView with evaluateJavascript

No comments:

Post a Comment