Skip to content

Instantly share code, notes, and snippets.

@AgentKnopf
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AgentKnopf/59288bd78b314830dffe to your computer and use it in GitHub Desktop.
Save AgentKnopf/59288bd78b314830dffe to your computer and use it in GitHub Desktop.
Usage of ConfigurationManager in Login Fragment
//This class resides in my app project (not the library project)
public class Login extends Fragment {
@Inject
ConfigurationManager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ApplicationBootstrap.INSTANCE.getApplicationComponent().inject(this);
}
@Override
public void onResume() {
super.onResume();
//An NPE occurs here - i.e. manager is null
String message = manager.getMessage();
}
}
//The ApplicationComponent looks like this
@Component(dependencies = {LibraryComponent.class}, modules = {AdditionalAppModule.class})
@ApplicationScope
public interface ApplicationComponent extends LibraryComponent {
//Here is the stub for injecting the Login Fragment
void inject(Login loginClass);
}
//My ConfigurationManager looks like this:
@Singleton
public class ConfigurationManager {
private final String message;
@Inject
public ConfigurationManager() {
//Initialize message
message = "Hello world";
}
public String getMessage() {
return message;
}
}
//My library component
@Component(modules = {LibraryModule.class})
@LibraryScope
public interface LibraryComponent {
ConfigurationManager configurationClass();
}
//My Library Module
@Module
public class LibraryModule {
@LibraryScope
@Provides
public ConfigurationManager configurationClass() {
return new ConfigurationManager();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment