So I have this class that extends the Thread class. I use the thread to connect my phone to a Bluetooth device and keep the connection on.
Once the connection is established (or once the user interacts with the GUI) I need to pass to another Activity.
I’d like to carry with me the thread just created so that I could interact with it (stop it, restart it, modify its status).
Should I make this thread implements Serializable (or Parcelable) and append it as a normal Extra to the Intent calling the other Activity?
Is it possible / correct / convenient ?
If the answer is not, I’d like to know why and which is the most correct method to implement such thing.
Thank you.
Well, Thread is independent from Activity, regardless the fact it’s i.e. created there or its inner class (in if your Thread keeps reference to first activity, you are most likely leaking the memory there). If you want your thread fully independent from Activity, you should consider simply separating that functionality. One way is by extending
Applicationand move all the code there. Then no matter what, as long as your application object exists, your thread could be easily reachable thru it. Other option is, as CommonsWare reminded, to just use regularServiceand delegate this Bluetooth communication job to it. Whichever you choose, it will be much simpler and better than trying to pass Thread here and there.