square / dagger

A fast dependency injector for Android and Java.

Home Page:https://square.github.io/dagger/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Holding reference after onDestroy()

jemshit opened this issue · comments

Question: does Dagger remove reference to injected variables on onDestroy() of Activity ? Here a guy wrote Activity holds reference to injected Presentervariable after Activity is destroyed. Someone enlighten me, thanks.

Dagger knows nothing about Android and thus nothing about the Android activity lifecycle. Injected types are not automatically freed.

However, you interpreted that issue incorrectly. It is the presenter which is keeping a reference to the activity and preventing the activity from being garbage collected, not the other way around.

So we make each injectedVariable=null on onDestroy() or any other quick
solution to prevent memory leak? I thought @ActivityScope etc.. on Components destroy all injected variables after that scope (Activity) is destoyed?
On Fri, Feb 19, 2016, 16:59 Jake Wharton notifications@github.com wrote:

Dagger knows nothing about Android and thus nothing about the Android
activity lifecycle. Injected types are not automatically freed.

However, you interpreted that issue incorrectly. It is the presenter which
is keeping a reference to the activity and preventing the activity from
being garbage collected, not the other way around.


Reply to this email directly or view it on GitHub
#519 (comment).

There is no leak. If the activity has outgoing references to dependencies and there are no incoming references to the activity it will be garbage collected.

Ok, so the problem was Presenter keeping ref to Activity. Thanks 👍

One last question, after Activity is destroyed and so its Presenter is garbage collected, does Presenter still can hold references even it is garbage collected?

No. Once garbage collected an object doesn't hold any references.

On Fri, Feb 19, 2016 at 2:07 PM Jemshit Iskenderov notifications@github.com
wrote:

One last question, after Activity is destroyed and so its Presenter is
garbage collected, does Presenter still can hold references even it is
garbage collected?


Reply to this email directly or view it on GitHub
#519 (comment).

I guess the thing i missed is Activity is destroyed does not mean it is garbage collected.