Ad

Retaining Dagger 2 Subcomponent

- 1 answer

Is it possible to retain a Dagger 2 subcomponent for an activity through a configuration change? It seems like the only two possible solutions are:

  • to use a retained Fragment, or
  • to save the state of the objects in the subcomponent and restore it using the savedInstanceState Bundle.

Both seem like messy solutions - has anyone solved this problem with a cleaner solution?

Ad

Answer

Your question sounds like something that you should not try to do.

A component that is created and dependent on the activity will likely have a reference to the activity. Retaining that component sounds like a primary cause for memory leaks.

When your configuration changes, the activity is destroyed and everything associated with it should also be destroyed.

So what you mentioned—saving the state in the bundle onSaveInstanceState and recreating it again—sounds like the cleanest solution to me.

Ad
source: stackoverflow.com
Ad