1

I have couple of (nested) fragments, for which I'd like to persist arbitrary data. I don't want to use regular singletons, because then the memory would never be freed, even after leaving particular part of the app.

I've been researching Dagger and Mortar, but I can't think of a way to have child object graph that is tied to a fragment, but which is also retained across configuration changes - most examples just show object graph per activity, but that doesn't help at all (since it'd be destroyed farily quickly).

The only thing I could think of are either storing the graph in a retained fragment, which doesn't actually help, or in Application class. But then how can I know when should I free the graph?

Overall my problem is how to use Dagger's child object graph in Android ecosystem, where everything except from Application is destroyed every once in a while

wasyl
  • 3,421
  • 3
  • 27
  • 36
  • Define 'every once in a while'. – nhaarman Mar 05 '15 at 08:31
  • @NiekHaarman I meant that I can't really expect anything, except for an `Application`, to stay alive during usage. `Activity` will be recreated on orientation change, fragments as well; sometimes fragment deep in the backstack will get destroyed etc etc – wasyl Mar 05 '15 at 08:49
  • The purpose of the graph is to exist as long as the application does, so it should be in the Application class. You want to use Mortar to inject things into the graph, and they will get removed from the graph when they are no longer needed. The graph itself however, will and should continue to exist for use by other fragments/activities/what have you. – Cryptovirus Mar 05 '15 at 19:40
  • @Cryptovirus I do realise that, but Mortar is built on top of Dagger, isn't it? So the subgraphs (made with `plus`) would be possible to be removed? Overall the point is to have Mortar-like functionality, but I don't know how well it works with fragments, which I use a lot, unfortunately – wasyl Mar 06 '15 at 00:03

0 Answers0