mercredi 20 avril 2016

common runonUIthread for whole application

So, I am designing an android application that interacts with some hardware over a bluetooth dongle.

The request/response model is a little slow (sometimes it takes 1-2 seconds to receive a response to a particular request) and hence I have kept the request sent and response received model in a different thread.

To achieve the same I have created an interface ResultPoster with some callback methods like onMyResponse().

This interface is implemented by various Activity and UI components of my application, but as we know that we simply can't post to UI or Main thread directly from another thread therefore I use runOnUIthread in all the UI components (Activities or Fragments basically).

e.g.

runOnUiThread(new Runnable() {
@Override
public void run() {
stopProgressDialog();
launchActivity();
}
});  

There are a lot of such blocks present in my code now and they are increasing as some logic gets complex.

Now, I want to keep my code clean and create a bridge throughout the application that will help me to post results over to UI from another thread, just based upon the context that I am into. Can you suggest me some other approach ?

Aucun commentaire:

Enregistrer un commentaire