Last active
September 13, 2015 10:31
-
-
Save dpolishuk/97c3a23ea3534088408c to your computer and use it in GitHub Desktop.
HOWTO override module in dagger2
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 interface BaseActivityComponent { | |
} |
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 BusModule { | |
@Provides | |
@PerActivity | |
public Bus provideBus() { | |
return new AsyncBus(); | |
} | |
} |
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 DebugBusModule { | |
@Provides | |
@PerActivity | |
public Bus provideBus() { | |
return new DebugBus(); | |
} | |
} |
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
@PerActivity | |
@Component(modules = { ActivityModule.class, BusModule.class }, dependencies = AppComponent.class) | |
public interface ActivityComponent extends BaseActivityComponent{ | |
void inject(MainActivity 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
@PerActivity | |
@Component(modules = { ActivityModule.class, DebugBusModule.class }, dependencies = AppComponent.class) | |
public interface DebugActivityComponent extends BaseActivityComponent { | |
void inject(DebugActivity 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
public abstract class BaseActivity extends Activity { | |
private BaseActivityComponent component; | |
@Override protected void onCreate(Bundle state) { | |
super.onCreate(savedInstanceState); | |
this.component = createComponent(); | |
} | |
public BaseActivityComponent getComponent() { | |
return component; | |
} | |
protected BaseActivityComponent createComponent() { | |
MyApp app = (MyApp) getApplication(); | |
return DaggerActivityComponent.builder() | |
.appComponent(app.getComponent()) | |
.activityModule(new ActivityModule(this)) | |
.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 class DebugActivity extends BaseActivity { | |
@Inject | |
Bus bus; // после вызова inject() в onCreate сюда встанет DebugBus | |
// мы переопределяем createComponent от базового класса | |
// и в onCreate базового класса вызовется этот метод | |
@Override | |
protected BaseActivityComponent createComponent() { | |
MyApp app = (MyApp) getApplication(); | |
return DaggerDebugActivityComponent.builder() | |
.appComponent(app.getComponent()) | |
.activityModule(new ActivityModule(this)) | |
.build(); | |
} | |
@Override | |
protected void onCreate(Bundle state) { | |
super.onCreate(state); | |
setContentView(R.layout.activity_debug); | |
((DebugActivityComponent) getComponent()).inject(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment