0

I need advice for the next implementation.

I have an app, which has different distinct sections. Each has a target, more or less differentiated.

One of them is a chat. This chat is an Activity, which has 3 fragments, like Whatsapp, but much simpler.

When I start the activity, I start a service that connects to the XMPP server, users get, messages, invitations, etc ....

When I start a chat with a user, start a new activity. (This is important)

When the XMPP Service receives things, I'm sending through sendbroadcast the fragments and update information. So far so good, as both the activity and its three fragments I have them in memory, and I can go update their respective objects.

My problem is the next: When I open a chat with a user, open a new activity, and lose access to the objects that are needed me. (For example the user list, now if my service receives a request of friendship, I can not add, because the object does not have)

What should I do?

As far as I know 3 options:

1- Pass through intent on the objects to start, obviously I have to do parceables. And when you close the chat activity, return the updated objects.

2- create me a singleton object (as I read, many problems with memory loss)

3- Later, I'll have to create a SQLite to save the message history. Could I use this database to be keeping these objects temporarily?

Better you have a choice? Different recommendation?

Cœur
  • 37,241
  • 25
  • 195
  • 267
daicon
  • 181
  • 3
  • 15

1 Answers1

1

Both options 2 & 3 can be used to solve your problem. Option 2 is simpler and can be a first step in the implementation. However, if you are planning to use SQL in the future anyways, option 3 seems like the right option for you to choose. It can help you decouple the data storing part & fetching from the actual UI. As for your question about whether a DB can hold temporary data, you can definitely clean the relevant tables when you don't need them anymore (for example whenever you start the app and have old data).

A good reference I suggest you look at is CursorLoaders.

Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
  • Thank you very much for the reply. The truth that I stay a little more quiet, because I thought there was some magic thing that did not know for persistent objects between activities. Although I still do not like too much the singleton, I'll make a test for rapid deployment, and then I'll pass to SQLite. About CursorLoader, thanks for the info. Did not know him. I've been reading a bit, if I have not misunderstood: With a CursorLoader I can go updating a list that is in another activity from the current activity in the background? Is it so? – daicon Oct 11 '16 at 18:28
  • When you couple the CursorLoader with a ContentProvider you can update the data between several Activities (or other parts of your app). I think this answer can help you with that - http://stackoverflow.com/questions/15517920/how-do-cursorloader-automatically-updates-the-view-even-if-the-app-is-inactive – Doron Yakovlev Golani Oct 11 '16 at 18:40
  • Thank you very much. :) I have some doubt more: I understood that A ContentProvider is used only to share information with external applications. So is this? It never used one, but it's what I understand. – daicon Oct 11 '16 at 19:01
  • 1
    You don't have to expose your data to external applications - you can use android:exported="false" when you define it (see details in https://developer.android.com/guide/topics/manifest/provider-element.html) – Doron Yakovlev Golani Oct 11 '16 at 19:24