Simon
Simon

Reputation: 2246

AsyncTask as one of many steps not updating UI

In my case, I am needing to obtain some data from the network, process it and then get other data from the network based on the result of the first network query. However, the first query could produce the need for user intervention ... or not.

I understand how AsyncTask can run and then display it's result in a UI object. However, how should I "chain" actions together so that the first AsyncTask derived object correctly dies before starting a new action.

My thought is that when onPostExecute is called, if I start another UI based action, it will not terminate until the UI based action has terminated.

Is there a correct way to finish an AsyncTask process, signal the result to the calling object, clean up, calling object can then do its own processing ?

Or am I fussing about nothing here ?


EDIT : Please note my question is about AsyncTask background processing and how to correctly use it to avoid an execution stack overflow. I am not asking if I should use this method.

Upvotes: 1

Views: 181

Answers (3)

Simon
Simon

Reputation: 2246

The best way to do this I found, after a good amount of searching, is to use message handlers. This means that, at the end of the threads action it posts a message with the result to the UI code (not a UI widget note) and then can terminate cleanly.

This means a light and simple implementation.

Upvotes: 0

Thkru
Thkru

Reputation: 4199

UI Based Actions should never (!) block the workflow, maybe you should use ViewSwitchers or BackgroundServices and update the UI recently, instead of block sth.

Upvotes: 0

Andres
Andres

Reputation: 400

I think you should try using a background service for your network requests and update UI accordingly. You could use an intent service for the network operations and broadcast updates to the activity to refresh UI. Hope it helps.

EDIT: Also, about cancelling the requests, i think it is better to implement a Service and attach to it using bindService() on the Activity. That way, if there is a cancelling request, you could notify the Service to stop whatever it is doing.

Upvotes: 1

Related Questions