Android-CleanArchitecture icon indicating copy to clipboard operation
Android-CleanArchitecture copied to clipboard

Explanation of @PerActivity

Open entropitor opened this issue 11 years ago • 7 comments

This is not really an issue, as much as a question: How is the @PerActivity scope enforced?

I can never seem to find how exactly scopes work in Dagger 2. Could you may be eleborate more on that? (or provide a link where it is explained?)

entropitor avatar Apr 12 '15 19:04 entropitor

http://fernandocejas.com/2015/04/11/tasting-dagger-2-on-android/

android10 avatar Apr 12 '15 22:04 android10

So if I'm understanding it right, the @PerActivity will work exactly the same as @Singleton if we never recreate the ActivityModule?

entropitor avatar Apr 13 '15 10:04 entropitor

It would be nice to try that by the way...

android10 avatar Apr 13 '15 19:04 android10

This is actually one of my concerns with "Scoped" components in Dagger 2. The PerActivity, PerFragment or other annotations are leading you to think that Dagger will magically manage the scope of those components for you, but it doesn't.

So correct me if I'm wrong, but unless you destroy your component in Activity.onDestroy() (which might not get called anyway http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29), the component will not be garbage collected, and you will leak everything that's inside of it (probably your Activity).

And then, how does this even work if your Activity or Fragment is started twice? They will share the same component (the first one that was created). Which means that if you are using that component to provide an Activity instance to your fragments, this might return another Activity than the one your fragment is attached to. That is very scary to me and I wouldn't use custom scopes for that reason unless I knew exactly what I was doing...

nat-e avatar Aug 07 '15 21:08 nat-e

@nbarraille onDestroy is not guaranteed to be called only in the case the host process is being killed outright. Therefore, you won't leak anything - the hosting process dies along with all its ClassLoaders/statics, and there's nothing to leak.

If the Activity is started twice in the same process, onDestroy will be called.

cmc5788 avatar Sep 02 '15 00:09 cmc5788

@cmc5788 :+1:

android10 avatar Sep 02 '15 08:09 android10

If the Activity is started twice in the same process, onDestroy will be called.

Wait, what? No, this is not true, in general you can start one Activity as many times you want.

farmazon3000 avatar Aug 02 '17 13:08 farmazon3000