2

I have a bit complex scenario. Let me first post the code then explain FragmentModule

 @Module
    public class FragmentModule
    {
        @Provides
        public static PickerDashboardFragment providesPickerDashboard()
        {
            PickerDashboardFragment fragment = new PickerDashboardFragment();
            return fragment;
        }

        @Provides
        public static PickingFragment providesPickingFragment()
        {
            PickingFragment fragment = new PickingFragment();
            return fragment;
        }

        @Provides
        public static PickingItemsFragment providesPickingItemsFragment()
        {
            PickingItemsFragment fragment = new PickingItemsFragment();
            return fragment;
        }

        **Note this**
        @Provides
        public static StagedItemsFragment providesStagedItemsFragment(ArrayList<PickJava> pickedList)
        {
            Bundle arguments = new Bundle();
            arguments.putParcelableArrayList("picked_list", pickedList);
            StagedItemsFragment fragment = new StagedItemsFragment();
            return fragment;
        }
    }

Here is FragmentComponent

@Component(modules = {FragmentModule.class, FragmentUtilsModule.class})
public interface FragmentComponent
{
    void inject(PickerDashboardActivity target);

    void inject(PickerDashboardFragment target);

    void inject(PickingFragment target);

   **Note this**
    StagedItemsFragment stagedItemsFragment();
}

Very important part. Requesting injection and this is how i'm building my component inside Fragment.

    public class PickingFragment extends Fragment{

    FragmentComponent component

    @Inject
        PickingItemsFragment pickingItemsFragment;

    @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

      component = DaggerFragmentComponent
                    .builder()
                    .fragmentUtilsModule(new FragmentUtilsModule(this))
                    .build();
            component.inject(this);
    }

**Note this**
@Override
public void onClick(View v)
{
    // Here i need StagedItemsFragment stagedItemsFragment(); from FragmentComponent
replaceNestedFragment(component.stagedItemsFragment());
}

But the problem is that the fragmentComponent is already built. Now as you can see in the FragmentModule providesStagedItemsFragment method requires ArrayList<PickJava> pickedList as a argument. How can i pass this. Please help or ask more details if required.

eC Droid
  • 651
  • 2
  • 9
  • 26
  • It was already explained to you in an answer to one of your previous similar questions that it is not a good idea to provide Fragments in modules. Is there any reason why you think that advice is invalid? – David Rawson Jul 21 '17 at 10:32
  • @DavidRawson sorry for late replying to that one. Please check my comment over there. – eC Droid Jul 21 '17 at 11:14
  • But for now please tell me how to achieve above. – eC Droid Jul 21 '17 at 11:14
  • The first step is to stop providing Fragments in the modules and use static factories instead. Please study the sample project that is linked in the previous question. – David Rawson Jul 21 '17 at 11:16
  • I have a demo, I can change my implementation later but please tell me how to do above using Dagger – eC Droid Jul 21 '17 at 11:21
  • This is not a duplicate. It's a lot different scenario. Please stop marking everything duplicate without even reading the whole question. – eC Droid Jul 21 '17 at 11:22
  • @DavidRawson just suppose the above module is not a fragmentmodule and is different class then how to achieve above using dagger. Please try to understand i can't change the implementation right now. – eC Droid Jul 21 '17 at 11:25

0 Answers0