The app I'm building has two worker threads, one for networking and one just for receiving network data. The problem I'm having is that I'm not sure how to save the Socket, BufferedReader, and PrintWriter objects from the networking thread before I close it in onPause(). I don't need the objects to persist, I just need a way to save them in a way that doesn't leave the worker threads running if Android kills the activity in onPause(). Is there a way to save these objects to a Bundle for onSaveInstanceState(), or is there another way (like onRetainNonConfigurationInstance(), but not just for config changes)? Also, will Android automatically close all of an application's threads if it kills the activity? That would solve my problem as well. Thank you.
You have a few questions in there. I will try to answer a few.
Threads, Sockets, BufferedReaders and PrintWriters cannot be Parceled. You can parcel a file descriptor but sadly not these complex types. These complex types have a lot of state and that would be hard to parcel to a bundle without knowing the implementation of each.
onPause() is not the same as a kill. onPause simply means your process is not going to be running foreground. So you basically will have no view. However your threads will still be there, and running unless you do something to them. If you are going to manage your own threads, I would recommend keeping references to them so that you can call interrupt() on them when you need them to stop. (Or if you extended the thread, have some sort of exit condition in it's run method that can be triggered by some custom method on it)
if your app is truly killed, then yes your threads are killed too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With