0

I have 2 different fragments and they both will be using by 2 different activities. In both activity's layout there is one frame layout to use as fragment container.

Which are;

Fragments: MapFragment, ListFragment

Activities: HomeActivity, SearchActivity.

Lets talk about HomeActivity;

HomeActivity will inject ListFragment and MapFragment on onCreate of activity and push injected fragments to it's layout(list fragment first) and after that, activity will call injected fragment's presenter method to show service result on list or map.

Codes; Fragment Side;

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

Activity Side:

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);

  }
}

Update: Problem is; I need to inject challengeListFragment in HomeActivity on initComponent method, also I need to give challengeListFragment object to ChallengeListFrgModule as parameter in same place. I think problem occurred in here. Before finish injecting ChallengeListFragment object ChallengeListFrgModule constructer triggered. How can I fix it

Murat
  • 415
  • 7
  • 17

0 Answers0