Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

5
Better practice for using evaluateJavascript
Post Body

Hey guys!

I'm working on a simple app where I want to wrap a web client. So far everything is going amazing, but I have one little issue. I end up using webView.evaluateJavascript(), and for overall better architecture design I made separate class for two types of evaluateJavascripts.

One without callback:

public static void send(WebView webView, String eventScript) {
        webView.evaluateJavascript(eventScript, null);
        Logger.withTag(TAG).log(" Event send to client "   eventScript);
    }

And one with callback:

public static void sendAsync(WebView webView, String eventID) {
        webView.evaluateJavascript(eventID, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String result) {
                // Do what you want with the return value
                Log.d(result, " Received");
            }
        });

    }

So my problem here is that I need to pass the WebView as parameter. Is there a way to avoid that.

First thing came to my mind is to make WebView global, but the IDE warns me for memory leaks.

So yeah...kinda small problem but still I would like to make this better. Maybe I should try some design pattern?

Author
Account Strength
100%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
86,816
Link Karma
18,003
Comment Karma
68,584
Profile updated: 6 days ago
Posts updated: 10 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
6 years ago