I solved this problem using a global listener. Each user in my app has a global listener where other users can write snips of data updates to. like "lastMessage sent when user is inactive". Since firebase is web socket having multiple listeners to the same reference is not a problem. Plus I try to solve the solution with a singleton but wasn't helpful.
1. just have a <global/userId> listener in the parent/root activity on create
2. or have the global listeners in each activities onResume and destroy in onPause. that also works.
3. have a <global/userId> listener in the parent/root activity on create. and have the same listener in other activity on create.
What I find convenient is using the builder pattern so the code to add in each activity is not overwhelming.
for example
//child added, child change etc
//I have attached a screenshot of my global listener view
globalChannel = new GlobalChannel.Builder()
.init()
.status(deviceUserId)
.addStatusListener(this)
.load()
.connect();
contextUserChannel = new UserChannel.Builder()
firebase reference - <userId/ConversationId>
.channel(contextUserId, conversationId)
firebase reference - active:true/false
.active(false)
keepSync feature
.sync()
value change listener
.addActiveListener()
firebase reference - type:true/false
.typingIndicator()
value change listener
.addTypingIndicatorListener(this)
start listening
.connect();
messageChannel = new MessageChannel.Builder()
.init(conversationId)
.chatBetween(deviceUserId, contextUserId)
.addMessageListener(this)
.loadAllPreviousMessages(this)
.connect();
//remove event listeners conveniently
messageChannel.disconnect();
contextUserChannel.disconnect();
globalChannel.disconnect();
makes it easier to develop and manage. hope this helps