Ankit Srivastava
Ankit Srivastava

Reputation: 29

Why on calling first fragment second fragment is automatically getting called?

I am using ViewPager to show 4 fragments but the problem is that on loading first fragment second fragment is also getting called i.e. the onActivityCreate() method of both the fragments is getting called together and on swiping to second fragment third fragment's onActivityCreate() and so on for other fragments and for last fragment no onActivityCreate() is getting called. How to resolve that.

Here are my files:

Purchase Details

public class PurchaseDetails extends AppCompatActivity implements ActionBar.TabListener {

    ViewPager viewPager;
    ActionBar actionBar;

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



        viewPager=findViewById(R.id.master_purchase_pager);

        FragmentManager fm=getSupportFragmentManager();

        actionBar = getSupportActionBar();
actionBar.setTitle("Purchase Details");
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        viewPager.setAdapter(new PurchaseFragmentAdapter(fm));
        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) {

            }
        });



        ActionBar.Tab tab1 = actionBar.newTab();
        tab1.setText("Date Selector");
        tab1.setTabListener(this);


        ActionBar.Tab tab2 = actionBar.newTab();
        tab2.setText("Week");
        tab2.setTabListener(this);


        ActionBar.Tab tab3 = actionBar.newTab();
        tab3.setText("Month");
        tab3.setTabListener(this);


        ActionBar.Tab tab4 = actionBar.newTab();
        tab4.setText("Year");
        tab4.setTabListener(this);



        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
        actionBar.addTab(tab4);


    }

    @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) {

    }
}

PurchaseFragmentAdapter

public class PurchaseFragmentAdapter extends FragmentPagerAdapter {
    public PurchaseFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment=null;

        if(position==0)
        {
            fragment= new PurchaseDateSelector();
        }
        else if(position==1)
        {
            fragment= new PurchaseWeek();
        }
        else if(position==2)
        {
            fragment= new PurchaseMonth();
        }
        else if(position==3)
        {
            fragment= new PurchaseYear();
        }


        return fragment;
    }

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


}

PurchaseDateSelector

public class PurchaseDateSelector extends Fragment
{


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


return inflater.inflate(R.layout.purchase_date_selector_fragment,container,false);
    }




    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Toast.makeText(getActivity(), "Purchase Date Selector", Toast.LENGTH_SHORT).show();


}

}

PurchaseWeek

public class PurchaseWeek extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.purchase_week_fragment,container,false);


    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Toast.makeText(getActivity(), "Purchase Week", Toast.LENGTH_SHORT).show();

    }


}

PurchaseMonth

public class PurchaseMonth extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.purchase_month_fragment,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Toast.makeText(getActivity(), "Purchase Month", Toast.LENGTH_SHORT).show();

    }
}

PurchaseYear

public class PurchaseYear extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.purchase_year_fragment,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Toast.makeText(getActivity(), "Purchase Year", Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 1

Views: 1607

Answers (3)

Amrish Kakadiya
Amrish Kakadiya

Reputation: 1023

The ViewPager doesn’t create all its pages at once. When using a lot of pages this would be horribly slow and even unnecessary if the user would never swipe through all these pages. By default the ViewPager only creates the current page as well as the off-screen pages to the left and right of the current page.

enter image description here

So what is solution of this problem, Is their any?

yeah there is,

mViewPager.setOffscreenPageLimit(int limit);

Parameters : limit – How many pages will be kept offscreen in an idle state.

ViewPager require a minimum of 1 offscreen pages

If you set value to 0 then you should be getting a warning about this in LogCat, something like:

Requested offscreen page limit 0 too small;

defaulting to1 Its because ViewPager is based on concept of keep ready next page to be loaded. ViewPager can not the page which not exist still.

Thanks to Tech Code Geek

Upvotes: 1

Rajan Kali
Rajan Kali

Reputation: 12953

That's the default and expected behavior of view pager, it creates each fragment to its left and right along with the visible fragment for smooth animations, you can't restrict fragment from creation, may be what you can do is restrict the loading content on non-visible fragments using setuserVisibleHint() method

@Override
public void setUserVisibleHint(boolean visible){
   if(visible){
       //load content
    }
}

Upvotes: 0

skynet
skynet

Reputation: 726

This is how ViewPager has been designed to work. By default the ViewPager only creates the current page as well as the off-screen pages to the left and right of the current page. This is to allow smooth transition between the pages.

You can also set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.

Read More

Upvotes: 0

Related Questions