Home > Back-end >  The background ended but i'm not on the main thread
The background ended but i'm not on the main thread

Time:04-29

So here is the flow of the app

  • Start a background thread and give it the this as listener

  • doinbackground method do some work than it calls Listener.finishfun The problem is not here . The problem is that when this methods is called i test the following code

    Looper.getMainLooper()==looper.mylooper it gaves true

Meaning that im not on the main thread . How is that possible. Why did i test that : because i can not call any method that runs on the main thread only and i get errors like glid

java.lang.IllegalArgumentException: You must call this method on the main thread

progress view also show throws a similar error

any one can explain??

CodePudding user response:

The callback is called from whatever thread that calls it, not from the thread that happened to instantiate it or was responsible for passing it to the background thread.

If you want to call your callback on the main thread, you need to post it to a main thread Handler. For example:

Handler(Looper.getMainLooper()).post {
    myCallback(someReturnValue)
}

CodePudding user response:

My bad i didn't call the callback from onpost Method

  • Related