views:

77

answers:

3

hi there!

in my android app i have an ui-update-thread that keeps all my views up-to-date.

protected Thread UIUpdateThread = new Thread()
{
 @Override
 public void run()
 {
  while(true)
  {
    query_some_data_from_service(); // gets some "fresh" data from a service
    UIUpdateHandler.sendEmptyMessage(0); // will update all ui elements with the new values
    sleep(1234);
  }
 }
};

i start this thread in onCreate() with

UIUpdateThread.start();

and everything works just fine :) when i leave the activity (e.g. because i switch to another activity) i call

UIUpdateThread.interrupt();

within onStop() to prevent the thread from running all the time. if i dont do this, it would keep running even if i close the app !?!?!!

problem is: how do i bring this thread back to life when returning from some other activity to this one? run() doesnt work, calling the initial start() in some other method like onResume() crashes the app.

i already tried a lot of things, but nothing seems to work :(

+1  A: 

in normal Java (and I think this applies for Android as well), a Thread-Object cannot be brought to life again once it has been stopped.

Atmocreations
+3  A: 

You can only called Thread.start() once per thread. Multiple calls to start() will throw Exceptions which I'm sure you've noticed.

This seems to be strange use case. Why do you have a thread running to update your UI? The main thread of the Android app is the UI thread and all UI updates have to be done on that thread.

Update:

Then you have it working backwards. Your service should be telling your app when to update. If you do it that way then you don't need a thread running in your app and your problem will be solved. You can use the ResultsReceiver pattern that let's you bind an Activity to your service and lets the service send results back to your activity. And whenever onCreate runs on your activity you can just re-bind to the service. Check out the Google I/O 2010 app for an example.

BrennaSoft
my app features a service and this thread periodically queries this service for updates on the data.
xenonite
updated my answer.
BrennaSoft
+3  A: 

Atmocreation's answer is the correct one...assuming that your implementation is a good idea in the first place.

First, the thread that you show here runs briefly and immediately terminates on its own, so you would not need to call interrupt() to stop it. In that case, you are probably better served switching to an AsyncTask rather than your own thread and handler, as you will need less code and can take advantage of AsyncTask's thread pool.

If your real thread is not what you show here, but some sort of infinite loop, that is a bad idea in general. Never create busy loops, particularly in Android. Most likely, there is a better solution for your problem, perhaps one that would avoid the need for interrupt() as well.

CommonsWare
i cut out almost everything of the threads body since it seemed to be irrelevant for demonstration purposes. the "original" thread features an endless loop, querying some data from a service in each round.a handler is then responsible to actually display the data fetched by the thread.since this thread is not running anymore on return to this activity, my ui is getting no more updates. :( this is why i need to somehow start the thread again...
xenonite
solved this - see my reply
xenonite
A timer is a better option for such tasks, especially on an embedded platform. busy-loops on a server are one thing, but on a phone with a itsy-bitsy battery, the OS can handle scheduling tasks at regular intervals much better.
Nate Bross