handling activity destruction in multithreaded android app
Posted
by Jayesh
on Stack Overflow
See other posts from Stack Overflow
or by Jayesh
Published on 2010-05-25T09:36:37Z
Indexed on
2010/05/25
9:41 UTC
Read the original article
Hit count: 246
android
|multithreading
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.
© Stack Overflow or respective owner