Comments (3)
Sorry I'm not entirely following. Aren't the dagger.android
docs talking about when you should inject the Activity while the Hilt docs are talking about when the Fragment is injected?
from dagger.
Hilt docs have a code snippet with Fragment
code, yes, but discuss any @AndroidEntryPoint
that has a parent, which means it could be an Activity
too:
If you mark an
@AndroidEntryPoint
class with@OptionalInject
(...) This gives you the chance to provide dependencies in a different way
dagger.android
docs talk about both too. Part about fragments is this:
before calling
super
(...)DaggerFragment
does the same inonAttach()
, which also prevents inconsistencies if theFragment
is reattached.
Furthermore, even if it was like you say, let's say we want to follow dagger.android
advice for an Activity
: If I try checking OptionalInjectCheck.wasInjectedByHilt(this)
before super.onCreate(savedInstanceState)
, then it will simply not work, because wasInjectedByHilt
will return correct value only if it is called after super
I edited OP to try and make this more clear
from dagger.
Ooh I got it now, thanks. This is a good question!
This is actually fairly tricky, but I think to do it you for an Activity you would want to register an OnContextAvailableListener
and do the injection logic in there. As long as your OnContextAvailableListener
is registered after the Hilt one (and this should happen because we register our listener in the Hilt base class's constructor, so even doing it in your constructor should put yours after ours since super constructors would be called first), then it should work. These listeners get called within the onCreate
of the Android Activity base class and so should also satisfy the dagger.android
docs where these are called before fragments are attached.
I can update the documentation, thanks for pointing this out!
from dagger.
Related Issues (20)
- Dagger 2.50 KSP fails to generate field in implementation for a @Named field HOT 4
- [KSP] dagger-android hanging indefinitely on a large multi module project HOT 2
- Optional bindings in a parent component can sometimes use bindings from child components HOT 2
- NullPointerException dagger.spi.shaded.androidx.room.compiler.processing.XTypeElement.getAnnotation HOT 11
- Problem with Injection of DAO HOT 1
- error: [Hilt] Could not get element for com.foo.viewModels.FooViewModel_HiltModules.BindsModule: java.lang.NullPointerException HOT 1
- Missing release notes for 2.51 HOT 1
- `LazyClassKey` doesn't work when providing a `Map<Class<*>, Provider<?>` with a scope
- @AssistedInject in ViewModel using Dagger (NOT Hilt, just dagger) does not compile: cannot be provided without an @Inject constructor or an @Provides-annotated method HOT 6
- [issue] Dagger doesn't compile or doesn't give a right error message when ViewModelFactory is implemened on Kotlin rather than on Java HOT 4
- Having an issue with Dagger Hilt Proguard Rules HOT 12
- Following dagger.dev tutorial doesnt create singleton in v2.51 HOT 2
- Multibinding with Nested Complex Keys uses the wrong AnnotationCreator HOT 1
- [Hilt] onCreate() in my Application doesn't called on Instrument test HOT 4
- [Dagger-Hilt] Error when applying obfuscation other than Proguard (Allatori) HOT 3
- error: [Hilt] Expected @AndroidEntryPoint to have a value HOT 3
- @IntoSet analog for injectable classes HOT 3
- Hilt Generated Code having not used code? HOT 2
- [Dagger-Hilt] Can't get viewModel in composable by Hilt HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dagger.