需要帮助理解为什么将Splash Presenter作为null返回。我遵循干净的架构。
public class SplashActivity extends BaseActivity implements HasComponent<LoginComponent> {
@Inject
SplashPresenter splashPresenter;
private LoginComponent loginComponent;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
//setContentView(R.layout.spla);
this.initializeInjector();
if (savedInstanceState == null) {
// addFragment(R.id.fragmentContainer, new UserListFragment());
}
splashPresenter.initialize();
}
private void initializeInjector() {
this.loginComponent = DaggerLoginComponent.builder()
.applicationComponent(getApplicationComponent())
.activityModule(getActivityModule())
.loginModule(new LoginModule())
.build();
}
@Override
public LoginComponent getComponent() {
return loginComponent;
}
}
在我的活动中,SplashPresenter始终为空。
这是活动组件
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
//Exposed to sub-graphs.
Activity activity();
}
@Module
public class ActivityModule {
private final Activity activity;
public ActivityModule(Activity activity) {
this.activity = activity;
}
/**
* Expose the activity to dependents in the graph.
*/
@Provides @PerActivity Activity activity() {
return this.activity;
}
}
这是我的LoginComponent
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = {ActivityModule.class, LoginModule.class})
public interface LoginComponent extends ActivityComponent {
}
这是LoginModule
@Module
public class LoginModule {
private User user;
public LoginModule(){}
public LoginModule(User user){this.user = user;}
@Provides
LoginRepository provideLoginRepository(LoginDataRepository loginRepository) {
return loginRepository;
}
@Provides
@PerActivity
@Named("isUserLoggedIn")
UseCase provideIsUserLoggedInUseCase(IsUserLoggedIn isUserLoggedIn){
return isUserLoggedIn;
}
@Provides
@PerActivity
@Named("login")
UseCase provideLoginUseCase(LoginRepository loginRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread){
return new Login(user,loginRepository,threadExecutor,postExecutionThread);
}
@Provides
@PerActivity
@Named("logOut")
UseCase provideLogoutUseCase(LoginRepository loginRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread){
return new LogOut(user,loginRepository,threadExecutor,postExecutionThread);
}
}
在BaseActivity中我正在做以下
public abstract class BaseActivity extends Activity {
@Inject Navigator navigator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getApplicationComponent().inject(this);
}
protected ActivityModule getActivityModule() {
return new ActivityModule(this);
}
}
仍然我无法理解为什么我的演示者在SplashActivity中为null
这是我的Splash Presenter
@PerActivity
public class SplashPresenter implements Presenter {
private final UseCase isUserLoggedIn;
private SplashView splashView;
@Inject
public SplashPresenter(@Named("isUserLoggedIn") UseCase isUserLoggedIn) {
this.isUserLoggedIn = isUserLoggedIn;
}
}
答案 0 :(得分:0)
loginComponent
的实例在inject method
之后创建。
将组件初始化程序移动到自定义Application类。或者将组件初始值设定项移至BaseActivity
,inject method
至SlashActivity
并且您要在Component接口处为每个要注入实例的Activity创建inject declaration
。