0

I have a number of activities and a service which works on a different thread. For example, the service has received a response from the server and it sends it to the active activity. BUT the app may not be active when the response from the server is received. I want to save my objects from the response somewhere and then when the app is opened again, to send a notification and the currently active activity gets the response.

I know I can use DB, but it will be redundant in this case because I need to save only the last response. I was thinking about creating a DB, but doing so for only one object is not entirely reasonable.

Can you advise a smart way how can I do this? Thank you for your time.

Slavi Petrov
  • 320
  • 6
  • 12
  • [SharedPreferences](https://developer.android.com/training/basics/data-storage/shared-preferences.html)? – Bö macht Blau Jul 22 '17 at 20:08
  • I can conclude from the answer of this post([https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences]) that for this it is necessary to serialize the array of objects, which is not the best idea. i have some idea to put my array to static List in App.class. What do you think about this? – Денис Тараненко Jul 23 '17 at 11:16
  • Personally, I somehow don't like using static variables. I'd prefer [saving the data to some File](https://developer.android.com/guide/topics/data/data-storage.html#filesInternal). And questions like [this](https://stackoverflow.com/questions/40595803/android-static-variables-are-lost) suggest your approach may not work reliably. It's not that the answers explain why it can't work, it's only that someone tried using static variables and somehow things did not work like expected – Bö macht Blau Jul 23 '17 at 17:28
  • Hm... I understand you. It's a very good advice and I would try to implement it in project. But, writing/reading to/from file will be too slow, how do you think? P.S. Sorry for my English and thank you for link. – Денис Тараненко Jul 24 '17 at 18:13
  • Maybe use a combined approach: if your app is in the foreground pass the data to the Application class (and save it as well, maybe with an IntentService). If the app is not in the foreground, well, how large is your payload? I'm afraid you'll have to run the app on various devices to find out if loading the data when the app starts takes too long. And don't worry about your English, it's very much ok and will get even better with time. BTW I'm German, so I know my English never will be the "real thing". But if it's sufficient to make myself understood, then: mission accomplished :) – Bö macht Blau Jul 24 '17 at 18:27
  • You say smart things :) The service in my application runs in the background. Therefore, I think I need to use the save in a file. Thanks for the help. It was very helpful for me. – Денис Тараненко Jul 25 '17 at 03:44

1 Answers1

0

Put the data in a database. Even if this seems like overkill, this is the easiest and most robust approach. SQLite is very lightweight anyway. You can either serialize the data into a string or byte stream and store it that way, or you can create a structured schema for the data and store it as multiple rows, columns, tables, etc.

David Wasser
  • 93,459
  • 16
  • 209
  • 274