2

Based on a button click, I have to do some processing that requires some time. So I decided to do this in a separate thread from the main UI thread.

Now, based on the calculations in the separate thread, I call a function in the main class of UI thread from which this new thread was created. In this function, I update the UI. I was told that this won't work as I need to call the main UI thread.

Could someone please help me with this?

@Override
public void onListItemClicked(int index, Map<String, Object> data) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            // Issue command() on a separate thread
            wasCommandSuccess(command());
        }
    }).start();
}


private void wasCommandSuccess(boolean result){
    if (result == false){
        getUI(BasicUI.class).showAlert("Command failed!", "Unable to access");
    }
}
Krypton
  • 3,337
  • 5
  • 32
  • 52
Sunny
  • 7,444
  • 22
  • 63
  • 104
  • You would usually use AsyncTask for this. I'm willing to bet a simple search on the Web would have told you that, though. See http://stackoverflow.com/q/4369537/1856738 – class stacker Apr 10 '13 at 09:21
  • My class already extends another class – Sunny Apr 10 '13 at 09:26
  • Yes, and so what? You had to implement your Thread, so you'll be able to implement an AsyncTask, no? – class stacker Apr 10 '13 at 09:28
  • Why should he switch to AsyncTask while he is already using Thread? Yes, you have a nicer name as AsyncTask, but they actually do the same thing. Each person has his own preference, using Thread is not worse choice than AnsyncTask. – Krypton Apr 10 '13 at 09:35
  • Look, if you don't want to use `AsyncTask`, then you'll have to use one of the ways to run a Runnable which has a reference to the results on the UI thread. One method us `Activity.runOnUIThread()` but it requires that you have a reference to an `Activity` object. – class stacker Apr 10 '13 at 09:39
  • @Krypton I can't agree that `Thread` and `AsyncTask` are the same thing. In fact, they're completely different, because AsyncTask resides two abstraction levels above Thread. But besides technical aspects, the OP appears to be a beginner and AsyncTask is not only more efficient but also much clearer, so he'll be able to have less trouble in the future, without losing anything. – class stacker Apr 10 '13 at 09:48
  • You are right they are not the same thing, I only say that they do the same thing :) – Krypton Apr 10 '13 at 09:50
  • Well yes, @Krypton, if you ignore the fact that AsyncTask uses a managed Thread pool, thus avoiding expensive overhead, and also deals with your Handler automagically, allowing you to forget about how to get back to your UI thread -- if we forget about these minor details, then yes, sure, "AsyncTask and Thread do the same thing". ;) – class stacker Apr 10 '13 at 10:08

1 Answers1

1

You should call the function wasCommandSuccess in runOnUiThread(); So you should have code like this:

@Override
public void onListItemClicked(int index, Map<String, Object> data) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            // Issue command() on a separate thread
            final boolean result = command();
            // you need to pass your context (any of Activity/Service/Application) here before this
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    wasCommandSuccess(result);
                }
            });
        }
    }).start();
}


private void wasCommandSuccess(boolean result){
    if (result == false){
        getUI(BasicUI.class).showAlert("Command failed!", "Unable to access");
    }
}
Krypton
  • 3,337
  • 5
  • 32
  • 52