我想用一个主要活动和多个片段制作一个简单的项目。在这里,我在一个活动中有两个片段,我想将演示者注入登录片段,但它不起作用。我的错误在哪里?
主应用程序
public class MainApplication extends DaggerApplication{
private static ApplicationComponent component;
@Override
public void onCreate() {
super.onCreate();
Utils.init(this);
}
public static ApplicationComponent getComponent() {
return component;
}
protected AndroidInjector<? extends DaggerApplication> applicationInjector()
{
component =
DaggerApplicationComponent.builder().application(this).build();
component.inject(this);
return component;
}
}
主活动.java
public class MainActivity extends DaggerAppCompatActivity {
private Fragment[] mFragments = new Fragment[2];
private int curIndex;
@Inject
HomeFragment homeFragment;
@Inject
LoginFragment loginFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
curIndex = savedInstanceState.getInt("curIndex");
}
mFragments[AppConstant.HOME_FRAGMENT] = homeFragment;
mFragments[AppConstant.LOGIN_FRAGMENT] = loginFragment;
FragmentUtils.add(getSupportFragmentManager(), mFragments,
R.id.container, curIndex);
showCurrentFragment(AppConstant.LOGIN_FRAGMENT);
}
private void showCurrentFragment(int index) {
FragmentUtils.showHide(curIndex = index, mFragments);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle
outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putInt("curIndex", curIndex);
}
}
应用组件.java
@Singleton
@Component(modules = {
ContextModule.class,
ApiServiceModule.class,
AndroidSupportInjectionModule.class,
ActivityBuilder.class
})
public interface ApplicationComponent extends
AndroidInjector<DaggerApplication> {
void inject(MainApplication component);
@Component.Builder
interface Builder {
ApplicationComponent build();
@BindsInstance
Builder application(MainApplication application);
}
}
慕标琳琳
相关分类