Search code examples
androidandroid-activityactivity-stack

Android, what is more efficient? Hide activity or start new with old data


Hi I am developing the chat app for android (just private project)

  • User has contact list in which he can choose the contact(lets call this activity contact_activity).
  • When he chooses contact, next activity (lets call it chat_activity) opens and there he is able to write messages with other users, but when user presses back button, and restores same chat room again, everything from previous active conversation is deleted.
  • I want to store and show data received for every active conversation user enters to , until he closes the conversation manually or the application is closed.

What solution is more optimal?

  • To pass data from every chat_activity back to contact_activity and then pass it back when user "restores" chat_activity by creating new activity
  • I heard about some magic with activity stack, so to minimize activity and then somehow restore it with old data context.

Thanks for your suggestions.


Solution

  • I'm not familiar with the magic you're talking about ...

    But it's not activities job to hold data, their job is to show them.

    Solution i propose is that you have a ChatManager, for instance and it's a singlton. This manager could hold some sort of data structure for all your active chats [ones that were opened since you opened the app], maybe a Map whose key is a model object that represents the Contact your chatting with and the value could be a List of Messages belonging to that chat session.

    Activities "save and restore" technique isn't meant to be keep data between activity completely being finished and opened later, it's meant to keep data during a configuration change of some sort or having your activity killed by the OS due to low memory or something ..