从Activity 中调用Fragments演示者的方法

我有2个不同的片段,他们都将通过2个不同的活动使用。 在两个活动的布局中,都有一个框架布局用作碎片容器。

哪个是;

碎片:MapFragment,ListFragment

活动:HomeActivity,SearchActivity。

让我们谈谈HomeActivity;

HomeActivity会将ListFragment和MapFragment注入到onCreate活动中,并将注入的片段推送到它的布局(首先是列表片段),之后活动将调用注入片段的呈现器方法在列表或地图上显示服务结果。

代码; 片段一面;

@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(); } } 

ChallengeListFragment:

 @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) } } 

活动方面:

HomeActivityComponent:

 @CustomScope @Component(dependencies = NetComponent.class, modules = { HomeActivityModule.class, HomeFragmentManagerModule.class, ChallengeListFrgModule.class, HomeNearbyMapFrgModule.class }) public interface HomeActivityComponent { Context context(); void inject(HomeActivity activity); } 

HomeActivityModule;

 @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(); } } 

HomeActivity:

 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,同样需要将challengeListFrgModule作为参数提供给ChallengeListFrgModule。 我觉得问题发生在这里。 在完成注入ChallengeListFrgModule构造器之前触发ChallengeListFragment对象。 我怎样才能解决它