Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AsyncTask and Thread life cycle

I am a little confused about AsyncTask and Thread life cycle. What happens to a processes threads when the OS enters the OnStart/OnStop and onResume/onPause sequences. Are the threads affected by this sequence. I ask this question because I am using sockets in the threads and killing the threads will kill the tcpip connections as well (I assume). If the thredas are not killed then how do I 'reconnect' to them especially as they may will be 'connected' to an activity via a Handler.

like image 958
bogflap Avatar asked May 30 '26 18:05

bogflap


1 Answers

Threads are not automatically killed when the app goes to onStop or onPause, however, there is no guarantee it won't be killed. Android will keep it running until it needs the resources that the thread is using.

Generally, if you want a persistent connection when an Activity closes, then put that in a Service. You can run threads in it just like an Activity (to prevent blocking that usually occurs with connections), and it's far less likely Android will kill it.

like image 175
DeeV Avatar answered Jun 01 '26 07:06

DeeV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!