我有 2 个不同的片段,它们都将被 2 个不同的活动使用。在这两个活动的布局中,都有一个框架布局用作片段容器。
哪个是;
片段:MapFragment、ListFragment
活动:HomeActivity、SearchActivity。
让我们谈谈 HomeActivity;
HomeActivity 将在 Activity 的 onCreate 上注入 ListFragment 和 MapFragment 并将注入的 Fragment 推送到其布局(首先是列表 Fragment),然后,Activity 将调用注入的 Fragment 的 Presenter 方法以在列表或地图上显示服务结果。
代码;片段侧;
@CustomScope
@Component(dependencies = NetComponent.class, modules = {ChallengeListFrgModule.class, ChallengeRepositoryModule.class})
public interface ChallengeListFrgComponent{
void inject(ChallengeListFragment fragment);
}
_
@Module class ChallengeListFrgModule(private val mView: ChallengeListFrgContract.View) {
@Provides internal fun providesChallengeListFrgContractView(): ChallengeListFrgContract.View {
return mView
}
}
ListFrgPresenter:
public class ChallengeListFrgPresenter implements ChallengeListFrgContract.Presenter {
//region Variables
ChallengeListFrgContract.View mView;
ChallengeRepository challengeRepository;
// Application application;
private ChallengeSearchCriteria challengeSearchCriteria;
//endregion
//region Constructer
@Inject public ChallengeListFrgPresenter(ChallengeRepository challengeRepository,
ChallengeListFrgContract.View mView) {
this.mView = mView;
this.challengeRepository = challengeRepository;
}
//endregion
//region Override Methods
@Override public void load() {
}
@Override public void clearChallenges() {
mView.clearChallenges();
}
}
挑战列表片段:
@Singleton
class ChallengeListFragment/*@Inject*/
: BaseFragment(), ChallengeListFrgContract.View {
@Inject
lateinit var challengeListFrgPresenter: ChallengeListFrgPresenter
///...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initComponent()
}
fun initComponent() {
DaggerChallengeListFrgComponent.builder()
.netComponent((activity.applicationContext as DuupleApplication).netComponent) // crash
.challengeListFrgModule(ChallengeListFrgModule(this as ChallengeListFrgContract.View))
.challengeRepositoryModule(ChallengeRepositoryModule(activity))
.build()
.inject(this)
}
}
活动方面:
家庭活动组件:
@CustomScope @Component(dependencies = NetComponent.class, modules = {
HomeActivityModule.class, HomeFragmentManagerModule.class, ChallengeListFrgModule.class,
HomeNearbyMapFrgModule.class
}) public interface HomeActivityComponent {
Context context();
void inject(HomeActivity activity);
}
家庭活动模块;
@Module public class HomeActivityModule {
private final HomeActivityContract.View mView;
private final Context context;
public HomeActivityModule(HomeActivityContract.View mView, Context context) {
this.mView = mView;
this.context = context;
}
@Provides @CustomScope Context provideContext() {
return context;
}
@Provides @CustomScope HomeActivityContract.View providesActivityContractView() {
return mView;
}
@Provides @CustomScope ChallengeListFragment providesChallengeListFragment() {
return new ChallengeListFragment();
}
@Provides @CustomScope HomeNearbyMapFragment providesHomeNearbyMapFragment() {
return new HomeNearbyMapFragment();
}
}
家庭活动:
public class HomeActivity extends DrawerActivity
implements NavigationView.OnNavigationItemSelectedListener, HomeActivityContract.View {
//region widgets
Toolbar appToolbar;
ImageView imageViewHamburger;
private DrawerLayout drawerLayout;
//endregion
//region variables
@Inject ChallengeListFragment challengeListFragment;
@Inject HomeNearbyMapFragment homeNearbyMapFragment;
@Inject ChallengeListFrgPresenter challengeListFrgPresenter;
@Inject HomeNearbyMapFrgPresenter homeNearbyMapFragmentPresenter;
//...
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initComponent();
///...
pushFragment(challengeListFragment, FragmentHelper.homeListFragmnetIndex);
}
protected void initComponent() {
DaggerHomeActivityComponent.builder()
.netComponent(((DuupleApplication) getApplicationContext()).getNetComponent())
.homeActivityModule(new HomeActivityModule(this, this))
.challengeListFrgModule(
new ChallengeListFrgModule((ChallengeListFrgContract.View) challengeListFragment))
.homeFragmentManagerModule(
new HomeFragmentManagerModule(HomeActivity.this, manager, R.id.placeHolder_HomeAct))
.homeNearbyMapFrgModule(
new HomeNearbyMapFrgModule((HomeNearbyMapFrgContract.View) homeNearbyMapFragment))
.build()
.inject(this);
}
}
更新:问题是;我需要在 initComponent 方法的 HomeActivity 中注入challengeListFragment,还需要将challengeListFragment 对象作为参数提供给ChallengeListFrgModule。我认为这里发生了问题。在完成注入 ChallengeListFragment 对象之前,ChallengeListFrgModule 构造函数被触发。我该如何解决