0

I have an application where I am getting the message Choreographer: Skipped ## frames! The application may be doing too much work on its main thread. where ## is a two-digit number, usually around 40 or 50. Looking on stack overflow for this problem, I found a nice answer to this question: The application may be doing too much work on its main thread. A section in that answer stated:

How to fix it

Fixing this requires identifying nodes where there is or possibly can happen long duration of processing. The best way is to do all the processing no matter how small or big in a thread separate from main UI thread. So be it accessing data form SQLite Database or doing some hardcore maths or simply sorting an array – Do it in a different thread

Since my app involved using a class extending WebView to display LaTeX, I'm assuming that getting the information for the WebView on the main thread was causing the problem. However, this involved manipulating the Views that caused the error Only the original thread that created a view hierarchy can touch its views. The answer to the question linked above had a paragraph on that also:

Now there is a catch here, You will create a new Thread for doing these operations and when you run your application, it will crash saying “Only the original thread that created a view hierarchy can touch its views“. You need to know this fact that UI in android can be changed by the main thread or the UI thread only. Any other thread which attempts to do so, fails and crashes with this error. What you need to do is create a new Runnable inside runOnUiThread and inside this runnable you should do all the operations involving the UI. Find an example here.

However, Activity.runOnUiThread(Runnable runnable) supposedly runs it on the UI thread, and after researching, I found that the UI thread was the same thing as the main thread, so when I used it, it didn't help at all. So, my question is, if I want to initialize/load a WebView on a different thread to avoid slowing down my application, how do I achieve that?

Note: I've also tried using AsyncTask to achieve this, but it slows down the main thread as well (I know that the AsyncTask execution is the one slowing down the main thread when I tried it because when I removed it, there was no Choreographer error). Also, AsyncTask is deprecated and I want to avoid it anyways.

null_awe
  • 483
  • 6
  • 17
  • Generally you should use systrace to analyze what is going on with your main thread to see what it is doing. – JoxTraex Dec 31 '20 at 23:00

1 Answers1

0

I have solved my own problem. Doing some more debugging, I found out that KaTeXView.setText(String text) (KaTeXView extending WebView) was causing the Choreographer message. By using WebView.post(Runnable runnable), the Choreographer message no longer appeared. :)

null_awe
  • 483
  • 6
  • 17