6

I have an activity that may have threads running when the user presses back and finish()es the activity. What happens to those threads at that point? Will they all attempt to complete unless I interrupt them in onDestroy()?

For example, is the below code unsafe, because my views and cursor may be destroyed if the activity finishes before the thread?

The reason I ask is that I have occasional crashes when finishing activities that I haven't successfully debugged yet, because they happen rarely and never while I've been in debug mode. I have since started checking if my view objects are null before doing anything to them in runOnUIThread(). Not sure if that's the cleanest solution, or if that is the problem at all.

new Thread()(
public void run(){
    crunchOnSomethingForAwhile(mCursor);
    MyActivity.this.runOnUIThread(new Runnable(){
        public void run(){
            mTextView.setText("thread complete");
            mCursor.close();
        }
    }
}
).start();
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • What does LogCat say when these crashes occur? – alexanderblom Feb 10 '11 at 17:36
  • They've only happened when I was away from my computer, so I wasn't connected to LogCat. Maybe I'm missing something really basic, but when I try to dump my log later in the Windows console, I only get the last couple pages worth of logs in the console window--I guess it's running out of room. Need to figure out how to dump the file as a file to my PC. – Tenfour04 Feb 11 '11 at 17:34

2 Answers2

1

I would look at using AsyncTask instead of a simple Thread. It has some built in support for doing work in another thread, and then upon finish doing something on the UI thread.

That said, there are a number of threads around that talk about how to deal with AsyncTasks when the activity ends. What exactly you should do depends on what the task is doing. If it is only applicable to that Activity, then you can just cancel the task in onPause (assuming it is not just a rotation change).

Otherwise, there are various tactics for holding on to the AsyncTasks across Activities. See this question for some ideas. This commonsware blog also has an example of holding onto an AsyncTask across a screen rotation.

Community
  • 1
  • 1
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • Thanks for the info. I'm kind of locked into threads instead of asynctasks, because I'm using the Facebook SDK, which is written with mostly threads, and don't want to rewrite a bunch of it if I can avoid it. Not a big deal, but I don't see yet any benefit to switching it over. Regardless, it seems the result on the UIThread would be similar. Is manipulating a view in a destroyed activity unsafe? – Tenfour04 Feb 10 '11 at 20:00
  • I haven't actually tried it, but it seems very likely that trying to manipulating a view in a destroyed activity would have unintended consequences. – Cheryl Simon Feb 10 '11 at 20:14
1

Someone posted an answer on another thread that onDestroy sets all view references to null, and finally the activity reference to null. So I will assume that it is best practice to interrupt all running threads in onDestroy, or at least catch NullPointerExceptions in any runInUIThread methods.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • It would great if you linked that said thread. I'm currently looking for reference on this.. – FirstOne Mar 21 '19 at 13:26
  • @FirstOne The one from 8 years ago? :) I'm afraid I don't remember where I read that. But you can read the AOSP source code and find out. – Tenfour04 Mar 21 '19 at 16:47
  • I posted the comment in case someone comes across this and already knows.. I didn't expect you to remember, really xD – FirstOne Mar 21 '19 at 17:01