Last active
August 29, 2015 14:27
-
-
Save AgentKnopf/59288bd78b314830dffe to your computer and use it in GitHub Desktop.
Usage of ConfigurationManager in Login Fragment
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
//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