views:

60

answers:

1

Hi,

I have a multithreded app where background threads are used to load data over network or from disk/db. Every once in a while user will perform some action e.g. fetch news over network, which will spawn a background AsyncTask, but for some reason user will quit the app (press back button so that activity gets destroyed). In most such scenarios, I make appropriate checks in the background thread after it returns from n/w i/o, so that it won't crash by accessing members of the activity that is destroyed by now. However some corner cases are left where crashes happen, because the background thread would access some member of activity that is now null.

Do other Android developers have some generic/recommended framework to handle such scenarios?

These are the times when I wish android would have guaranteed termination of all threads when activity destroys (in the same way that regular linux process cleans up when it's quit)... but I guess Android devs had good reasons for not exposing process lifetimes through the api.

A: 

Personally I have a boolean "isAlive" that I set to false in onDestroy(), which is checked by the background threads. It has worked fine for my apps that has daemons syncing the application data with an online database.

sandis
Yes, that's what I use too. And if nothing better exists, I will extend the same logic to fix my corner cases.
Jayesh