0

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.

Smarty77
  • 1,208
  • 3
  • 15
  • 30

2 Answers2

1

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 ..

elmorabea
  • 3,243
  • 1
  • 14
  • 20
  • Okay i managed to create static Map in class describing contact_activity, indexing it with contactitem hashcodes, then passing correct index to class describing chat_acitivty where i always put existing arrayadapter(better said my custom adapter) into map with current index (aka hashcode). No need to keep whole activity alive, static Map is the answer. – Smarty77 Oct 22 '14 at 19:54
0

The best solution in my opinion is to store activities and then to restore them.

If needed you can also update them on restore by overriding some methods.

Check this post for more details about storing activities: Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
Klapsa2503
  • 829
  • 10
  • 33