简体   繁体   中英

Dagger singleton preloading

I want to perform such functionality:

  1. User opens first activity, and under the hood Dagger starts to initialize singleton, which I will inject on second activity
  2. Navigates to the second activity, and here I inject this singleton to the activity

I've tried to just stupid inject this class to the first activity, but it not seems like a good solution

All that is needed because I want to perform some local database request in this singleton, and if it is not ready when activity has already started it can be a little bit confusing for user

If you want the dependency to be available (=="preloaded") before the "second" Activity is started, you will have to @Inject this dependency somewhere else before. It can be the "first" Activity or eg the Application class.

In general it's not the best approach. It would be better if you weren't preloading dependencies, but loading them when they are actually needed. Loading one dependency can result in a whole spiral of dependency creation (hence the whole idea behind the dependency injection).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM