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
-keep class android.arch.** {*;} | |
-keep class * implements android.arch.lifecycle.LifecycleObserver {*;} |
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 LoginRegisterActivity extends AppCompatActivity { | |
@Inject | |
LoginRegisterPresenter mPresenter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
//... | |
mComponent = DemoApplication.getAppComponent(this) // AppComponent |
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
@ActivityScope | |
// 使用@Subcomponent 而不是 component | |
@Subcomponent(modules = {LoginRegisterActivityModule.class}) | |
public interface LoginregisterComponent { | |
// 这里是关键之处,SubComponent.Builder 由外部提供实现, | |
// 这个过程就是引入外部依赖的过程,至于引入了什么外部依赖, | |
// 我们完全不用关心,这样我们在 LoginRegisterActivityMoule | |
// 解析依赖时就从这个外部外部实现中搜索。这也就是为什么 SubComponent | |
// 优于 Module dependency 的地方 |
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
@ActivityScope | |
@Module | |
public class LoginRegisterActivityModule { | |
private final LoginRegisterActivity mActivity; | |
LoginRegisterActivityModule(LoginRegisterActivity activity) { | |
mActivity = activity; | |
} | |
@ActivityScope |
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
DemoApplication extends Application { | |
private AppComponent appComponent; | |
@Inject | |
TokenInterceptor tokenInterceptor; | |
@Inject | |
RealmConfiguration realmConfiguration; | |
@Override | |
public void onCreate() { | |
super.onCreate(); |
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 注解的类即为 Dagger2 的一个 component, | |
// 在注解中要声明包含的Module,后面会解释这些 Module 是怎么被使用到的 | |
@Component(modules = { | |
AppModule.class, | |
NetworkModule.class, | |
DemoApiModule.class | |
}) | |
// Component 必须是接口或者抽象类,Dagger2 框架会为你实现, | |
// Component 有两类抽象方法。↓ |
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 //使用 @Module 标记的 class,就成为 Dagger 2的一个 Module | |
public class AppModule { | |
private final DemoApplication mDemoApplication; // extends android.app.Application | |
// Module 的构造函数即普通的 Java class 构造函数,可以有任意个, | |
// 也可以包含任意构造参数。一个 Module 的构造函数调用, | |
// 即一个模块的初始化,这样说有点含糊不清,先放着继续往下看 | |
public AppModule(DemoApplication demoApplication) { | |
mDemoApplication = demoApplication; | |
} |