Android: keep activity working while in backstack


I have an chat app that goes like this :

Activity A ( main screen showing some general stuff)

Activity B ( chat room where user can send and receive messages using a WebSocket. I made websocket global, instantiated/reconnected in B, and singleton, since putting in Service seems to be a problem - not reliable, restarting sometimes where the socket it holds becomes null etc. and needs reinitialized etc.)

Activity C ( user navigates here from B, by clicking on menu. C shows list of participants, owner of the room can kick someone out etc.)

So, when I am in C, I have arrived as A-> B -> C.

When I am in C, I still want B to continue receiving messages, processing them, putting them in messagesadapter etc. That is because, we dont want to lose chat messages ( which are processed in B) while in screen C.

Question - when I am in C, will B functionality keep working ? I want to make sure it works. What do I need to ensure it happens reliably ?


Question - when I am in C, will B functionality keep working ? I want to make sure it works. What do I need to ensure it happens reliably ?

To answer your question, your Activity B is alive in the task and asynchronous code in that Activity will continue to run. Just because the Activity is "paused" does not mean that methods cannot be called on it. It is still a live object.

If you only want your chat active when the user has your application in the foreground, then you can do it the way you have described. As soon as your application goes to the background (on arrival of an incoming call, for example), Android can kill the process without warning. When the user returns to your app, Android will create a new process for it and Activity C will be reinstantiated. However, Activity B will not be reinstantiated.

However, you really need a Service to handle socket connections, as it is much more stable.

There are 2 ways to achieve this:

  1. When user moves from activity B to activity C, close the socket connection and save the "timestamp" of the last message in preferences. Now when the user is in activity C, your server can push the updates lets say "SYNC" through FCM (or GCM) which tells your app that there is a new update on the server. On receiving this notification from server, call the server pass the "timestamp" stored from the preferences to server. Now, server's job is to return the list of messages after the passed "timestamp". Store all the message in local database. Once the user comes back to the activity B, load the new messages from the local database.

  2. Keep the socket connection maintained when moving to another activity. Reconnect to the socket connection on opening of activity C.

You can check our open source android chat sdk code for reference:

The best solution would be create an IntentService which will keep your socket connection alive and keep fetching the messages in the device and when you go back to activity B, in onResume you can fetch all the latest messages form the local storage and update the list adapter.

By : Anirudh

This video can help you solving your question :)
By: admin