Open mingfai opened 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.
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.
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.
I'm going to leave this open as a reminder.
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 thegetSupportingFragmentManager
. Instead, it generated code that donew FragmentManager()
.As a workaround, we can do:
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.