0

My app consists of tab layout and each tab has is its own fragment. I have never encountered the need for a "fragment container" until i needed to refresh the current fragment when its parent AppCompatActivity clicked the back button.

Here is my Code for When I want to Refresh the Fragment:

GlobalFeedTab fragment = (GlobalFeedTab)
    getFragmentManager().findFragmentById(R.id.fragment_container);


getFragmentManager().beginTransaction()
    .detach(fragment)
    .attach(fragment)
    .commit();

PageAdapter:

      public class PagerAdapter extends FragmentStatePagerAdapter {
int mTabs;

public PagerAdapter(FragmentManager fm, int tabs) {
    super(fm);
    this.mTabs = tabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            GlobalFeedTab tab1 = new GlobalFeedTab();
            return tab1;
        case 1:
            CountryFeedTab tab2 = new CountryFeedTab();
            return tab2;
        case 2:
            CountryList tab3 = new CountryList();
            return tab3;
        case 3:
            NotificationsTab tab4 = new NotificationsTab();
            return tab4;
        case 4:
            ProfileTab tab5 = new ProfileTab();
            return tab5;
        default:
            return null;
    }
}


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

}

MainActivity:

            public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);




    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.earthicon));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.flagicon));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.listicon));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.notificationsiconi));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.profileicon));
   // tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });



   }
}
grantespo
  • 2,233
  • 2
  • 24
  • 62

2 Answers2

0

If you don't know your fragments ID, you can use the method FragmentManager.findFragmentByTag(String), where the tag is a String you either put in your fragment's XML attributes or in the fragment transactions add or replace calls.

pdegand59
  • 12,776
  • 8
  • 51
  • 58
0

1. ((ViewGroup)getView().getParent()).getId(); 2. Checkout out this answer: link

Community
  • 1
  • 1
asethi
  • 146
  • 12