I am trying to inject a FragmentManager and the associated fragments but 
the OnCreateView of the fragments inside tablayout is never called.Also,

manager.getFragments().isEmpty() returns true;

MainActivityModule:
@Module
public  class MainActivityModule {

    @Provides
    static FragmentManager getFragmentManager(MainActivity mainActivity) {
        return mainActivity.getSupportFragmentManager();
    }
    @Provides
     ViewPagerAdapter getViewPagerAdapter(FragmentManager fragmentManager){
        return new ViewPagerAdapter(fragmentManager);
    }
    @Provides
    static FragmentOne getFragmentOne(){
            return FragmentOne.newInstance();
    }
    @Provides
    static FragmentTwo getFragmentTwo(){
        return FragmentTwo.newInstance();
    }
    @Provides
    static FragmentThree getFragmentThree(){
        return FragmentThree.newInstance();
    }

}
AppModule:
@Module
public abstract class AppModule {
    @ContributesAndroidInjector(modules = MainActivityModule.class)
     abstract MainActivity mainActivity();
    @Provides
    @Singleton
    static Context getContext(Application application){
        return application.getApplicationContext();
    }
    @Provides
    @Singleton
    static MainActivity getMainActivity(){
        return new MainActivity();
    }

}
AppComponent:
@Singleton
@Component(modules = {MainActivityModule.class, AppModule.class})
public interface AppComponent {
    void inject(MainActivity mainActivity);

    @Component.Builder
    interface Builder {
        AppComponent build();

        @BindsInstance
        Builder buildapp(Application application);

    }
}
MainActivity:
public class MainActivity extends AppCompatActivity {
    @Inject
    Context context;
    @Inject
    FragmentManager fragmentManager;
    @Inject
    ViewPagerAdapter viewPagerAdapter;
    @Inject
    FragmentOne fragmentOne;
    @Inject
    FragmentTwo fragmentTwo;
    @Inject
    FragmentThree fragmentThree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyTabApplication.getAppComponent().inject(this);
        ViewPager viewPager = findViewById(R.id.pager);
        viewPagerAdapter.addFragment(fragmentOne, "Frag1");
        viewPagerAdapter.addFragment(fragmentTwo, "Frag2");
        viewPagerAdapter.addFragment(fragmentThree, "Frag3");
        viewPager.setAdapter(viewPagerAdapter);
        TabLayout tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);


    }

}

class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    @Inject
    ViewPagerAdapter(FragmentManager manager) {
        super(manager);
        Log.d("manager", "" + manager.getFragments().isEmpty());

    }

    @Override
    public Fragment getItem(int position) {

        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return 3;
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
FragmentOne(all other fragments are similar):
public class FragmentOne extends Fragment {


    public FragmentOne(){
        Log.d("Fragmentone","one"); 

    }
    public static FragmentOne newInstance(){
        return new FragmentOne();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {

        Log.d("Fragmentonecons","one");
        return inflater.inflate(R.layout.fragment_one,container,false);
    }
}

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/60527b82-1261-4916-9bf1-168c4df5c387%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to