Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load the fragment activity oncreate() method of the actionbar selected tab only if the tab is selected?

I am using viewpager and actionbar tabs (4 tabs). When the Main activity starts, it loads the 4 fragments data.

how to make it load only the selected tab data?

Also when I select tab 1, tab1 and tab0 loads.

If I swipe between tab0 and tab1 , onstart() for both tabs not called.

If I select tab2 and then select tab0, onstart() for tab0 loads.

If I swipe between tab2 and tab3 , onstart() for both tabs not called.

MainActivity.java

MainPagerAdapter tabsPagerAdapter;
ViewPager viewPager;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager) findViewById(R.id.pager);
    tabsPagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(tabsPagerAdapter);

    actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    actionBar.addTab(actionBar.newTab().setIcon(getResources().getDrawable(R.drawable.doros)).setTabListener(this));
    actionBar.addTab(actionBar.newTab().setIcon(getResources().getDrawable(R.drawable.esteshara)).setTabListener(this));
    actionBar.addTab(actionBar.newTab().setIcon(getResources().getDrawable(R.drawable.aswak)).setTabListener(this));
    actionBar.addTab(actionBar.newTab().setIcon(getResources().getDrawable(R.drawable.akhbar)).setTabListener(this));


    viewPager.setOffscreenPageLimit(0);


    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.abs_layout);
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    viewPager.setCurrentItem(3);



}

 @Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

}


@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

}

MainPageAdapter.java

 public class MainPagerAdapter extends FragmentPagerAdapter implements PagerSlidingTabStrip.IconTabProvider{

public MainPagerAdapter(FragmentManager fm) {
    super(fm);
}

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

private int[] tabIcons = {
        R.drawable.ar,
        R.drawable.ar,
        R.drawable.ar,
        R.drawable.ar
};



@Override
public android.support.v4.app.Fragment getItem(int position) {
   switch (position)
   {
       case 3: return new NewsFragment();
       case 1: return new ConsultationFragment();
       case 2: return new IndecesFragment();
       case 0: return new LessonsFragment();
       default: return null;
   }
}



@Override
public int getPageIconResId(int i) {
    return tabIcons[i];
}

}

I found this link, but its not helpful link

like image 910
Mohammad Avatar asked Oct 31 '22 00:10

Mohammad


1 Answers

I have solved the problem.

I have created a static variable to store selected tab index.

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    sharedVariables.setSelectedTab(tab.getPosition());

Since Viewpager loads the selected page and the left page and the right page if they exist every time the page changed, so I override this function to check if this fragment is the one visible to user and then call onresume method.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
            onResume();
    } else {
        // Do your Work
    }
}
like image 144
Mohammad Avatar answered Nov 15 '22 06:11

Mohammad