I have an AppModule
class where I have the following method that return a FirebaseUser
object that I want to be available across my whole life-cycle of my app. Before using dependency injection I have used this code:
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
//Do stuff
}
Is it still necessary to check for nullity like this:
@Inject FirebaseUser firebaseUser;
//In onCreate()
if (firebaseUser != null) {
//Do stuff
}
Unless you explicitly add @Nullable
on your @Provides
annotated method Dagger will never inject null
values. If you want to inject a nullable object, then you need the @Nullable
annotation there as well.
Is it still necessary to check an object for nullity if it is initialized using Dagger2?
No, if your injection is set up correctly you will never have to deal with null
values, unless you explicitly want to do so. If you still get a NullPointerException, then you have a problem with your Dagger setup which you need to fix.