Issue
I am working on xmpp chat on Android now. I have next situation: 1) I have singletone class with XMPPConnection and 2 activities. 2) 1st activity is just a list of contacts, which I can choose and start message with. 3) In second activity(chat) I setup listeners for my connection and add some signal, which plays, when message from particular contact is received. 4) Then I close chat activity and onDestroy method is called. 5) And then, if I message to my app - I hear the signal, which plays in destroyed activity(I mean method which plays signal must be destroyed with chat activity).
Please, explain me why it's possible.
public class ChatActivity extends ActionBarActivity {
...
public void beep() {
Uri notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RingtoneManager.getRingtone(getApplicationContext(), notification).play();
}
...
public void setupListeners(XMPPConnection connection) {
if(connection != null) {
//Set a listener for chat messages
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null && message.getFrom()
.contains(chatModel.getRecipient())) {
chatModel.addMessage(StringUtils.parseBareAddress(message.getFrom())
+ " :" + message.getBody());
beep();
}
}
}, new MessageTypeFilter(Message.Type.chat));
//Set a listener for normal messages
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if(message.getBody() != null && message.getFrom()
.contains(chatModel.getRecipient())) {
chatModel.addMessage(StringUtils.parseBareAddress(message.getFrom())
+ " :" + message.getBody());
beep();
}
}
}, new MessageTypeFilter(Message.Type.normal));
}
}
Solution
Have you tried disconnecting the Connection in the onDestroy of your Activity? Or to remove the listeners? Because of these inner classes, which hold a (hidden) reference to their outer class - your Activity in this case, the Activity possibly can't be garbage collected. So removing the listeners or disconnecting the Connection should do the trick.
Edit: Like Gusdor said, you'd want to remove the listeners in the onPause() Method and add them again in the onResume().
Edit2: If you want that the XMPP Connection runs in the background, you should consider using a background Service for handling the Connection and the messages, instead of the Activity.
Answered By - tknell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.