powered by Jive Software

How do you maintain connection?

This is one of the thing I have been struggling. Where is an ideal place to store Smack connection? I have tried quiet number of options.

  1. Putting connection as app property
  2. Put it in Singleton and check if connected (and reconnect if not connected) on every onResume method of the Activities
  3. Other approaches I don’t currently even remember

How do you tackle this issue? Where is ideal or at least best place to store Smack connection?

TIA

That really depends on your design. But sometimes the simplest and most naive way of using a static field is sufficient, especially if you make the deliberate design decission to only support at most one XMPP connection at a time. Then you would have someting like

private static AbstractXMPPConnection connection;

and if you support multiple connections, then you could use a static map

private static Map<String,AbstractXMPPconnection connections;

Not sure what “app property” here refers to.

That sounds like a insufficient approach. I usually design a state machine for the connection management, that is feed with various events. The state machine then decides if it needs to act (e.g. restore connectivity). Event sources are e.g. CONNECTIVY_CHANGED listener for Android, and, of course, Smack’s own ConnectionListener.

1 Like

I intend to have single connection, and want something simple, so I think I will test this one first!

I meant property of custom Android application class. Will static field in Android application class be better than defining in activity? I only need simple messaging functionalities!

Thank you!
Somehow forgot to even think of this!