0

I have a JavascriptInterface and a setter/getter in my Android code:

mWebView.addJavascriptInterface(new AccessibleMethods(), "Android");

private class AccessibleMethods {
    private boolean isWorking;
    public void setIsWorking(boolean working)
    {
        this.isWorking = working;
        // A toast message here of this.isWorking displays true
    }
    public boolean getIsWorking()
    {
        return this.isWorking;
    }
}

In my JavaScript code I call the function:

Android.setIsWorking(true);

But when I try to access isWorking in Java in another method or outside the class, it doesn't have the correct value.

AccessibleMethods methods = new AccessibleMethods();
methods.getIsWorking(); // returns wrong or no value

I need this for accessing a JavaScript variable onKeyDown.

Any suggestions?

1 Answer 1

1

If you want to share the data passed from the JavaScript, you should use the same instance of the object across the method calls, or use an instance variable.

private AccessibleMethods methods = new AccessibleMethods();
//... 
mWebView.addJavascriptInterface(methods, "Android");
//..
boolean isWorking = methods.getIsWorking();
Sign up to request clarification or add additional context in comments.

1 Comment

If you think this can help anyone else, retain this; otherwise, you are free to delete it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.