Created
April 5, 2019 20:48
-
-
Save mitchtabian/49de015e3dbba3f930ea291ce903694d to your computer and use it in GitHub Desktop.
Example of making use of the Application context using @Component.Builder in AppComponent class (new way)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
@Component( | |
modules = { | |
AndroidInjectionModule.class, | |
AppModule.class, | |
ActivityBuildersModule.class | |
}) | |
public interface AppComponent extends AndroidInjector<BaseApplication> { | |
@Component.Builder | |
interface Builder { | |
@BindsInstance | |
Builder application(Application application); | |
AppComponent build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void init(BaseApplication app){ | |
appComponent = DaggerAppComponent.builder() | |
.application(app) | |
.build(); | |
appComponent.inject(app); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
public class AppModule { | |
@Singleton | |
@Provides | |
Drawable getDrawable(Application application){ // application is available here | |
return ContextCompat.getDrawable(application, R.drawable.ic_launcher_background); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Inject | |
Drawable getDrawable; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if(getDrawable == null){ | |
Log.d(TAG, "onCreate: drawable is null."); | |
} | |
else { | |
Log.d(TAG, "onCreate: drawable is NOT null."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment