-1

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

NevMem
  • 41
  • 4
  • If it's an asynchronous operation, there is no guarantee it will finish during the transition animation. Maybe show some loading thing while it's loading – EpicPandaForce Apr 05 '19 at 12:51

1 Answers1

0

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 e.g. 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).

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132