Last active
April 6, 2019 01:56
-
-
Save mitchtabian/1280843627a65106c8302858e258c86f to your computer and use it in GitHub Desktop.
Scope example with an @singleton User object and @LoginScope User object
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
@Override | |
public void onActivityDestroyed(Activity activity) { | |
if(activity instanceof LoginActivity){ | |
loginComponent = null; | |
} | |
} | |
private void handleActivity(Activity activity){ | |
if(activity instanceof HasSupportFragmentInjector){ | |
AndroidInjection.inject(activity); | |
} | |
if(activity instanceof HasActivityInjector){ | |
if(activity instanceof LoginActivity){ | |
loginComponent = appComponent.loginComponentBuilder().build(); | |
loginComponent.inject((LoginActivity)activity); | |
} | |
else{ | |
Log.d(TAG, "handleActivity: injecting an activity..."); | |
AndroidInjection.inject(activity); | |
} | |
} | |
} |
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 | |
@Provides | |
@Named("app_user") | |
static User provideUser(){ | |
return new User(); | |
} |
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 | |
@Named("login_user") | |
User loginUser; | |
@Inject | |
@Named("app_user") | |
User appUser; | |
Log.d(TAG, "onCreate: login user: " + loginUser); | |
Log.d(TAG, "onCreate: app user: " + appUser); | |
// log output | |
// onCreate: login user: com.codingwithmitch.daggerpractice.ui.login.User@3dd4a71 | |
// onCreate: app user: com.codingwithmitch.daggerpractice.ui.login.User@197e55f SAME | |
// onCreate: login user: com.codingwithmitch.daggerpractice.ui.login.User@e334e25 | |
// onCreate: app user: com.codingwithmitch.daggerpractice.ui.login.User@197e55f SAME |
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
@LoginScope | |
@Subcomponent(modules = { | |
LoginModule.class, | |
LoginActivityBuildersModule.class, | |
ViewModelsModule.class, | |
}) | |
public interface LoginComponent extends AndroidInjector<LoginActivity> { | |
@Subcomponent.Builder | |
interface Builder{ | |
LoginComponent 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
@LoginScope | |
@Provides | |
@Named("login_user") | |
static User provideUser(){ | |
return new User(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment