johncarl81 / transfuse

:syringe: Transfuse - A Dependency Injection and Integration framework for Google Android
http://androidtransfuse.org/
Apache License 2.0
220 stars 28 forks source link

Support injecting FragmentManager in FragmentActivity #42

Open mingfai opened 11 years ago

mingfai commented 11 years ago

There is an example that in a @Fragment class, it supports @Inject FragmentManager.

In a @Activity(type=FragmentActivity.class) class, it doesn't support injecting FragmentManager, and I expect it inject the getSupportingFragmentManager. Instead, it generated code that do new FragmentManager().

As a workaround, we can do:

 @TransfuseModule public static class MainModule{
        @Provides FragmentManager getFragmentManager(FragmentActivity fa){
            return fa.getSupportFragmentManager();
        }
    }

I do it in a similar way in Dagger, but I think Transfuse may potentially make it even simpler without the need to create a Module.

mingfai commented 11 years ago

My use case of FragmentManager is more complicated.

@Activity(type=FragmentActivity)
class MyActivity{
  @Inject @View(R.id.myViewPager) ViewPager pager;
  @Inject MyFragmentStatePagerAdapter adapter;

  @OnCreate  public void onCreate() {
    pager.setAdapter(adapter);
  }
}

MyFragmentStatePagerAdapter:

class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
       @Inject public MyFragmentStatePagerAdapter(FragmentManager fragmentManager) { 
        super(fragmentManager); //constructor required by super class
    }
}

This should be a very typical usage. For this case, the FragmentManager is not really use in the @Activity(type=FragmentActivity.class) class (that i suppose it should support align to the @Fragment case)

Is possible for MyFragmentStatePagerAdapter to get the "closest" FragmentActivity? (without a defined module) If it is supposed, we probably need to document it clearly so people can have a clue about where the FragmentManager comes from.

johncarl81 commented 11 years ago

Right now FragmentManager is available if you extend the android.support.v4.app.Fragment class without the need to use a @Provides. Looks like I missed including the FragmentManager available on the FragmentActivity and you're right, we can do better with Transfuse.

For your use case, could you try injecting the FragmentActivity into your FragmentStatePagerAdapter, and then getting the FragmentManager from there:

class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
       @Inject public MyFragmentStatePagerAdapter(FragmentActivity fragmentActivity) { 
        super(fragmentActivity.getFragmentManager()); //constructor required by super class
    }
}

I think this should do the work you mentioned of getting the "closest" FragmentActivity.

Let me know if this works for you and I'll see about making the FragmentManager move available.

mingfai commented 11 years ago

yes, it works. Better to inject FragmentActivity than use a Module. thx

p.s. I don't think better support for FragmentManager injection should be in a high priority.

johncarl81 commented 11 years ago

I'm going to leave this open as a reminder.