Reputation: 4464
I'm making Android Jabber chat client for school's work.
Everything works okay so far. But when I change the phone's orientation it will re-connect to server.
It only took 3-5 seconds to re-connect, but during that time if I send a message, it will cause NullPointerException
which is the XMPPConnection
variable.
By googling, I found that I can use onRetainNonConfigurationInstance
but my ChatClient
class extends Fragment
and it's not available.
My target is 4.0.3+ so I don't need to worry about compatibility support or such.
Any solution except using static
variable?
Thanks
Upvotes: 1
Views: 265
Reputation: 6862
I am not sure that hosting your xmppconnection inside the fragment is a good idea.
A common approach would be to host it inside a service so your connection can persist even if the application is in background (and in any case does not depend on your activity / fragment recreation), or, if you are not interested in having it connected when your app is in background, another good option would be to have the xmppconnection hosted inside a singleton object.
If, on the other hand, this is a school project and all you want is the fragment persist along configuration changes such as rotation, you can use setRetainInstance method:
Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack.
Upvotes: 1