0

In our project we are forbidden to store data from server (confidentional information). But once downloaded and parsed models are used in multiple fragments. Data on server updates once a day and forced logout occurs in midnight. I want to store parced models in memory and remove it at exit from app.

In current implementation I have two single-instance classes - RestClient and DataStorage, which in constructors are registered to Otto bus. I instance them in Application class:

@Override
public void onCreate() {
    super.onCreate();

    new RestClient(this);
    new DataStorage();
}

DataStorage for every model have metods:

@Subscribe
public void onModelComplete(Model model) {
    this.model = model;
}

@Produce
public Model produceModel() {
    return model;
}

Fragments send events throw bus to RestClient and receive results from RestClient or "produced" results from DataStorage (if data is received while app in background).

The problem is that when the user exits the application, data is not deleted because the application does not die. How to implement storing in memory with clearing data on exit without killing process?

anisart
  • 1,747
  • 2
  • 13
  • 15
  • 1
    that is because the notion of `exit` in android is not exactly this. you may want to add a clearing of your objects when you exit a specific activity, or upon a specific action from a user, but there are plenty of examples where the application stops to be on the foreground for a while, while not being considered exited (it can still actually be stopped even if you did not want it to, though) – njzk2 Jan 07 '15 at 14:59
  • Move `new RestClient(this); new DataStorage();` to `onResume()` to (re)instantiate instead and dereference any data when the app is in onPause method. – Nikola Despotoski Jan 07 '15 at 18:40
  • @NikolaDespotoski in this case data will be lost while user receive incoming call for example. But main reason for saving instances RestClient and DataStorage in background is storing response from server while app in background. – anisart Jan 07 '15 at 18:59
  • Yes, you are correct, but the objects will be reinstantiate when returned from call (`onResume()`). IMHO,this contradicts your requirement. If you want to clear memory whenever app is paused (but not killed/finished) implies that no data should be saved when activity is not in foreground? – Nikola Despotoski Jan 07 '15 at 19:04
  • @NikolaDespotoski OK, but then only loaded in background models will be in DataStorage. – anisart Jan 07 '15 at 19:24
  • You're in trouble. Because Android Java programs use a managed heap, there's no good way to clear data from memory without also exiting the process. And even then, model data will remain in memory until it is reclaimed for use by another process. – Jesse Wilson Jan 08 '15 at 00:40
  • @JesseWilson even if it is protected from another process (I know about it), I should have method to clear data at user logout (but not exit from app). – anisart Jan 08 '15 at 06:07

0 Answers0