1

I am currently facing a problem with persisting object state in an android app.

I have some kind of multi level master-detail flow.

For example, I have a list of Clients. Each Client has multiple Orders and each Order has multiple articles.

Until now I did the following:

  • When you click a Client in the MasterActivity I write the Client into the Bundle and open the DetailActivity.
  • In the DetailActivity I read the Client from the Bundle and display it.

This worked more or less fine most times, however it now happens, that an object is too big to be serialized (for example a Client with a lot of Orders).

Therefore I wanted to serialize only the ID of the Client and load it from the database inside the DetailActivity. Usually this approach works fine but I have one situation where it does not work:

The Articles are only saved, when you save the Order (their Master). So if you create a new Article for an Order and you don't save the Order, the Article isn't saved too.

That means, that I always need to have the full object, reloading it from the database inside the DetailActivity means, that I loose all the unsaved changes. However, persisting it, using the Bundles could exceed the limit (500kB to 1MB).

So my question is, what is the preferred way to persist such data?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Robert P
  • 9,398
  • 10
  • 58
  • 100

2 Answers2

2

A nicer way is to create your own singleton class which points to data you want to transfer between activities.

I suggest DataManager singleton activity which has Article articleToPass Make the setter set the articleToPass to point to what ever you want. and the getter or reader, will read fetch the article and nullify the articleToPass.

manage your app data using this DataManager singleton (This dataManager singleton can be initialized either in the extending Application class or MainActivity).

Incase you must persist the object when app is destroyed and loading back:

  1. Create from Article object a DB entry which contains all data you need (I see no reason why saving data you don't need here)
  2. Dump all data to some file (Using Shared Prefs, key and values)
  3. When entering the Details screen you want make a query to a sever of yours requesting all data you need by ID or such.
  4. Convert all object you need to JSON (simply using GSON), and dump the JSON to a file, and load it when you need.

I think the resolutions above are enough :) hope i helped

Mercury
  • 7,430
  • 3
  • 42
  • 54
1

You can subclass your own MyApp class from Application, specifying it in your manifest. This class is a singleton and is available all the time the android application is running like this (MyApp)getApplication().

So, in a field of this class you may keep some temporary data you need to keep between activities.

You have to check if this field is not null though because the OS can terminate your app anytime. In this case you will have to reload the data. You can keep some index of what data to be reloaded somewhere in the SharedPreferences.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • Thanks for this solution. When the user switches Application and Android decides to destroy my app, then I would loose all the data right? IF thats the case, how would you persist the data in that case? – Robert P Mar 28 '17 at 08:03
  • Android can terminate the app anytime so you anyway have to persist the data either in SQL Db or in SharedPreferences. This solution just lets you not to put the large data in Bundle since you will have a reference to them via Application almost all the time. And, in case your program got killed you will restore the latest data using some id you keep separately somewhere in SharedPreferences for example – Alexander Kulyakhtin Mar 28 '17 at 08:08