Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support injecting FragmentManager in FragmentActivity #42

Open
mingfai opened this issue Jun 26, 2013 · 4 comments
Open

Support injecting FragmentManager in FragmentActivity #42

mingfai opened this issue Jun 26, 2013 · 4 comments

Comments

@mingfai
Copy link
Contributor

mingfai commented Jun 26, 2013

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
Copy link
Contributor Author

mingfai commented Jun 26, 2013

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
Copy link
Owner

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
Copy link
Contributor Author

mingfai commented Jun 26, 2013

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
Copy link
Owner

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

@johncarl81 johncarl81 added this to the 0.3.0 Release milestone Aug 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants