The primary Android thread is obstructing the WebView thread

Recently, I've been tackling an issue involving making a synchronous call to JavaScript within a WebView, with the aim of retrieving a return value. However, pinpointing why this setup is not functioning properly has proven to be quite challenging for me. It appears that the WebView's thread is getting blocked while the main thread awaits a response from it - a situation that shouldn't occur as the WebView operates on a separate thread.

In order to shed some light on this problem, I have put together a concise sample code snippet:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:weightSum="1">

    <WebView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/webView"/>
</LinearLayout>
...

(Further details provided in the original text.)

Answer №1

Explore the process of migrating WebView on Android 4.4 for a better understanding. Check out the detailed information on Android Docs. It seems like you may need to consider using a different method to enhance your JS actions.

For instance, based on the documentation - Executing JS Asynchronously. This allows asynchronous evaluation of JavaScript code within the current page context. If there is a result, the |resultCallback| function will be triggered with the output. Remember that this method should be invoked on the UI thread and the callback will also be executed on the UI thread.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to assign attributes in a loop using an array?

I started with a simple one-dimensional array: var array = [1,2,3,4,5]; I needed to create a loop that would update each div id using values from my array: Initial IDs <div id="120"></div> <div id="121"></div> <div id="122"&g ...

Tips for effectively handling the deserialization of a one-to-many relationship with eager loading on both sides to avoid getting stuck in an endless

Here is the definition of my Product class: @Entity public class Product { @ManyToOne(fetch=FetchType.EAGER) @JoinColumn(name="listingGroup_id") @JsonBackReference public ListingGroup listingGroup; And this is the structure of my GroupPr ...

Sending a JSON object in the body of a post request using Retrofit 2.0

I have been working on sending a JSON parameter to an API, and I have achieved the following: {"v1" : "username", "v2" : "password"} Currently, I am sending 2 JSON objects with "v1" and "v2" as the parameters. Howev ...

The static HTML export encountered an issue: 'Error: Server Component type not supported: {...}'

Encountered an error while attempting to export a next js blog page to static html. My goal is to obtain the basic html structure with tailwind CSS from this blog template. Following the instructions on the next js documentation, I made changes to module ...

What is the correct way to incorporate QEventLoop into a std::thread?

Is there a way to ensure that an object containing a std::thread is finished when the object is destroyed? Here is a simple example: #include <thread> #include <future> #include <QEventLoop> struct Connector { Connector(std::string ...

An error will occur if you try to use $.ajax inside a function

I am facing an issue with a function that utilizes jquery's ajax to check an API for the success status of a variable. The goal is to return true if the "success" variable is true and false if it is false. Below is the code snippet: function checkLo ...

Determine how often each letter appears in a specific word

Can someone assist me in creating a program that counts how many times each letter appears in a given word, all displayed in alphabetical order? It should treat upper and lower case letters as equal, as well as work for numbers. For example: The program n ...

Steps for transforming a numerical value into an array with individual elements, such that the maximum value in the array will be 1

Can someone assist me? I have a value of 4.8 (it represents a rating of 4.8/5). Now I want to convert it into an array with the following structure: [1, 1, 1, 1, 0.8] What I mean is that the value should be split into 5 elements, with each element not ...

Manipulate the embed tag in Javascript to dynamically add text while the code is running

My HTML source code contains the following: <embed width="100%" id="video-player-flash"... The embed tag is enclosed within an iframe. I am trying to figure out how to access the embed element during runtime in order to append: <embed width="100% ...

Newbie looking to develop a Drag and Drop software

Although I am new to Android Development, I have a strong background in programming. I have already grasped some basic concepts such as fragments and OnClickListeners. For my initial android project, I am interested in creating an app that allows me to: ...

Is there a way to generate a .txt document utilizing Jquery?

I have a domain at example.com/index.html and I'm looking to create a .txt file within the domain called examples.com/file.txt that will store the HTML content below when clicking on the save button: .js: $('.saveButton').on('click&ap ...

Tips on using a dropdown menu to choose an item and automatically navigate to its specific details within an Angular framework

Here is the structure of my data : groups: [ { collections:[] }, { collections:[] }] I am utilizing nested *ngFor to showcase all groups and collections on a single page, which can make the list lengthy as more groups and collections are added. The hea ...

Stripes: Utilizing Table Pagination in HTML on a JSP Page

I'm currently utilizing the stripes java framework and have a jsp page that looks like this: <table id="mlTable" style=""> <col width="200"> <col width="250"> <thead> <tr> <th align="c ...

Cease running code post while loop (do not use break)

I need help with my code that checks if a user is already in a Database. Once the while Loop runs, it verifies the presence of the MC Player in their Database through the Mojang API. However, if the user is already in my Database, I want to skip the Mojang ...

Tutorial on transferring information between Java and PHP

Working on a new website, looking to connect with JAVA for encrypted information exchange. Any tips on how to achieve this seamlessly? ...

Execute the command to install metabase using the terminal prompt

Struggling to install Metabase using Windows 10 cmd prompt. nssm install metabase C:\Program Files\Java\jre1.8.0_144\bin\javaw.exe AppDirectory C:\Program Files (x86)\POS -jar metabase.jar After several attempts, I fina ...

Can you trigger a button click on one HTML page from another HTML page?

I am working on a project with two custom HTML pages: 'first.html' and 'second.html'. In 'second.html', there is a hidden div that I want to unhide when a button on 'first.html' is clicked. Both pages should be open ...

Every time the Select box choice is changed, Jade Pug will undergo a new iteration

Recently, I began using a Pug/Jade template to go through an object of events sent from an express app. Everything is working smoothly, but now I've added a select box with a dropdown populated by venues in the event object. I'm facing a seemingl ...

What is the best way to execute a task following a short delay?

I have developed an application that can quickly crop faces, but I want to create the illusion that the process takes a little longer than it actually does. Currently, when the user clicks a button, the dialog box appears showing the message "Cropping face ...

Is there a problem with certain devices receiving a HTTP/1.1 302 Moved Temporarily Error message?

Every time I try to download the txt file from my Google Sheet document on my Android 4.1.2 device, I encounter an HTTP/1.1 302 Moved Temporarily error. Interestingly, this issue doesn't occur on my emulator or a newer device running Android 6.0. Bel ...