-1

I want to fetch an user object from Database and have a single instance of the same in whole application. What would be best and efficient way to first fetch from database on app initialisation and make it available for whole class.

I am using dagger in project, if that helps. I already have DatabaseManager(fetch user object from database) injected in AppModule

user1288005
  • 890
  • 2
  • 16
  • 36
  • So how will you represent when the user is logged out? – EpicPandaForce Dec 19 '18 at 14:09
  • Possible duplicate of [What is an efficient way to implement a singleton pattern in Java?](https://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java) – shkschneider Dec 19 '18 at 14:11

1 Answers1

0

create a MyApplicationController which extends Application(), instantiate your dagger in this class, through a static method return an instance of this class, then in activity do MyApplicationController.getInstance().getAppComponent().inject(this.activity)

Mark your provides method with @singleton or create your own scope annotation. and most important add this to manifest file as your application class.

@Inject User

As long as you only have one instance of MyApplicationController you'll always have the same User through all your app.

have a look at this as well. Adding an Application controller to android project?