1

I have a long-running thread which I start on app startup:

public class MyHelper extends Thread {
    private AtomicBoolean mRun;
    public void run() {
        while (mRun.get()) {
            ...
        }
    }
}

public class App extends Application {
    private MyHelper mHelper;

    public void onCreate() {
        mHelper = new MyHelper();
        mHelper.start();
    }
}

We don't get any "app terminate" signal in Android though, so is the above keeping my app running forever in the background? There's no good place for me to know when to shut the thread down.

I wonder if the user pops the last activity from the stack (by hitting the back button), will my Application instance get destroyed even though the Thread is still possibly in a waiting state?

Thanks

user291701
  • 38,411
  • 72
  • 187
  • 285
  • Take a look here: http://stackoverflow.com/questions/680180/where-to-stop-destroy-threads-in-android-service-class – AlexS Feb 03 '12 at 19:51
  • Good link, we still have the problem that we don't get a signal when the app is wanting to be "terminated", so no way to know to shut our thread down. One user recommends setting Thread.setDaemon(true), seems like that's the only thing I can do, but seems a bit hacky. Luckily we're not doing any work in the thread when the app is backgrounded, but I'm just worried that the OS can't kill the app because the thread is suspended, and acting as a signal that the app should be kept alive? – user291701 Feb 03 '12 at 21:25

1 Answers1

0

to work around this, you can call thread.interrupt() in your ondestroy() method.

i've noticed that your activity can get destroyed but not necessarily your context.

user1159819
  • 1,549
  • 4
  • 16
  • 29