0
@Provides
@Singleton
LoginPresenter provideLoginPresenter() {
    return new LoginPresenterImplementation();
}

My approach when I design an Android app is MVP. I use Dagger for dependency injection. When I create an interface presenter I made it singleton.

Someone suggest me to not make it singleton cause it is heavy to memory.

What do you suggest? use it like singleton or not? I expect pro and against opinions.

ghita
  • 2,746
  • 7
  • 30
  • 54
  • Singleton annotation is optional, so you don't need to use it. And you can define any number of custom scope annotations in your application by declaring them as a public @interface. – kimkevin Sep 25 '17 at 09:36

1 Answers1

2

The Singleton in Dagger is the same as creating an instance and referencing it, but there are 2 point which you have to take care of:

  1. Having a singleton in Application class means creating an Object when ever the app starts and keep it in memory until the end which is necessary for some APIs but mostly you load some classes, use them in an activity and then you have to kill them after you are done with them cause it is a matter of memory management.

  2. Singleton annotation works fine when you add it to your Application module (the main one which you assign in Application class) but if you use it for submodules then you have to take care of keeping them alive.

You can take a look at this sample project http://github.com/mmirhoseini/marvel and this article https://hackernoon.com/yet-another-mvp-article-part-1-lets-get-to-know-the-project-d3fd553b3e21 to get more familiar with MVP.

Mohsen Mirhoseini
  • 8,454
  • 5
  • 34
  • 59