androidonpauseonbackpressed

How do you stop a Thread when the Activity it was started in is left?


I have looked at onBackPressed(), onPause(), and onStop() but the biggest issue I can find against these is that my Thread isn't declared in the Activity. I have MainActivity and TiltActivity. There is a Button that, when pressed, sends you from MainActivity to TiltActivity. Once inside of TiltActivity I create an object of the class TCP_IP_Client. I then start a 'Thread' that is declared inside of TCP_IP_Client.

I can't pass a Thread parameter to onBackPressed(), onPause(), oronStop(). How do I stop the Thread once TiltActivity is left if I can't interrupt it in the previous overridden functions. Again, the Thread is not declared in TiltActivity which would make it much easier to interrupt the Thread. If you need further clarification please let me know.


Solution

  • You need to add a method to TCP_IP_Client that will stop the thread that it declared and started. You then can call that method on your instance of TCP_IP_Client from your TitleActivity (using your member variable that holds the TCP_IP_Client instance).