Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:41:05+00:00 2026-06-08T02:41:05+00:00

I know that to interact from Javascript to Java you have to inject a

  • 0

I know that to interact from Javascript to Java you have to inject a Java object using the addjavascriptInterface method in webview.

Here is the problem I am facing.

  1. I register a java object using addJavascriptInterface method to be available in my JS.

  2. I inject few JS in the webview using webview.loadURL("javascript:XXX");

  3. I send a JS event when I am done with injecting the JS.

The problem is that if immediately after step 1, if I execute the following Javascript:

mWebView.loadUrl("javascript:if(window.myobject) console.log('myobject found---------'); else {console.log('myobject not found----');}");

I get “myobject not found” in my console’s log.

I want to know that if there is some time before I can access my object and if so, how do I get to know how much time should I wait to call my object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:41:07+00:00Added an answer on June 8, 2026 at 2:41 am

    I want to know that if there is some time before i can access my object

    Yes, I think there is a delay, because WebView.addJavascriptInterface will run in the WebView’s internal worker thread. Perhaps you’ve thought about this, and realized that WebView has to maintain at least one worker thread to do asynchronous network IO. Maybe you also noticed these threads in DDMS while using a WebView.

    It turns out that it also uses a thread to do work for a number of other public methods. I really wish the docs from Google made this clearer! But I hope I can help and show you how I tried to confirm this for myself.

    Follow me as I take a look at the source for WebView. It’s reasonably readable, even if you can’t follow exactly what’s going on, it’s possible to trace through answer some questions with respect to threads.

    You can download the Android framework source through the SDK manager tool, but it’s also mirrored on Github, so that’s what I’ve linked to here. I guessed and picked a tag that’s close to some version of ICS. It’s not hard to find WebView.addJavascriptInterface. I just Googled “WebView.java site:github.com/android”.

    The method WebView.addJavascriptInterface sends a message to an instance of WebViewCore:

    mWebViewCore.sendMessage(EventHub.ADD_JS_INTERFACE, arg);
    

    In WebViewCore.java there are a bunch of overloaded methods called sendMessage, but we don’t really need to know which exactly is being called, since they do pretty much the same thing. There’s even a nice comment to give us a hint that we’re in the right place! All of them are delegating to an instance of EventHub which is some inner class. This method turns out to be synchronized, and is sending a message to an instance of Handler, which is a good indication that this is probably running in another thread, but for completeness sake, let’s find out!

    That Handler is instantiated in EventHub.transferMessages which is called from WebViewCore.initialize. There are a few more hops here, but eventually I found out that this is called from run in WebCoreThread (subclass of Runnable), which is instantiated along with a new Thread right here.

    What an adventure! So, even though I really can’t say for sure what’s going on with all these moving parts, I am pretty confident to say that this method is not synchronous, and sends a message to the WebView’s worker thread. I hope that makes sense!

    if so, how do i get to know how much time should i wait to call my object?

    Unfortunately, I don’t know the answer to this. I was researching this exact issue and found this question on StackOverflow in the course of my Googling. I think you have the following options, some of which are nicer or easier than others:

    1) Just Thread.sleep for 100 ms or something between addJavascriptInterface and loadUrl("javascript:..."). Blech, I don’t like this, but it is potentially the easiest.

    2) Another possibility is that you could call WebView.loadUrl with a snippet of JavaScript that specifically tests if the interface is set, and catches the ReferenceError that is thrown if it’s not set yet. However, as you might have guessed, this kind of involves adding a JavaScript interface to the WebView!

    3) Call WebView.setWebChromeClient instead, and catch JavaScript’s alert() or console.log instead. From my experiments, this method is synchronous, so there is no delay. (I have confirmed this in source, but I’ll leave details as an exercise for the reader) You should probably come up with some special string to call alert with and check for it inside onJsAlert, so you aren’t just catching all alert()s.

    Sorry for the length of this answer, I hope that helps. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that Java have its own garbage collection, but sometimes I want to
I want to know if there is way to call javascript from java on
I know that conventionally for an app to interact with the internet, it must
I know that this sort of question has been asked here before, but still
I know that immutable objects always have the same state, the state in which
I know that in order to download a cookie associated with an account using
I'm just starting to learn Javascript; I do however have experience in programming using
I know it's possible to run JavaScript from the address bar, but are there
I have to interact with a set of web-services that each come with their
I am using Ruby on Rails 3 and I have a monolithic application that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.