I'm currently login to my MQTT and subscribe to a topic inside onCreate() method of my activity. However it disconnects from MQTT as soon as I start another activity. How can I login/connect and subscribe to MQTT once and use the same connection in multiple activities of my app to publish?
Asked
Active
Viewed 1,113 times
1 Answers
2
Run the MQTT connection in a Android Service and make calls against the service to publish messages and use either broadcasts or callbacks to receive subscribed messages.
In fact you don't have to implement any of this as there is already a Paho Android client that will do it all for you.

hardillb
- 54,545
- 11
- 67
- 105
-
I prefer not to use services since services stick to the background and consume resources. is there any there way to share the mqtt client between activities? – Shahab Intezari Jun 20 '17 at 11:52
-
Yes, use a properly written service – hardillb Jun 20 '17 at 11:53
-
what do you mean by properly? you mean I should destroy the service when my app closes? – Shahab Intezari Jun 20 '17 at 11:56