1

whenever i swipe to move to the next fragment the code inside another fragment gets executed tho the layout is loaded properly and the code also but it also executes the code of another fragment

ex: 3 fragments A,b,c

when i swipe from fragment A to fragment b : fragment b layout and code are executed but also fragment c code

when i swipe from b to c , only the code and layout of fragment c ,so its executed properly

so the problem is if it isnt the last fragment it calls the code of next one

here is my main_activty code

public class Main2Activity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

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


    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

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

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));


}



public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                home h1 = new home();
                return h1;

            case 1:
                status st = new status();
                return st;

            case 2:
                info info = new info();
                return info;

            case 3:
                setting set = new setting();
                return set;

        }
        return null;
    }

    @Override
    public int getCount() {

        return 4;
    }
}


}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dante
  • 75
  • 1
  • 9

1 Answers1

0

Your SectionsPagerAdapter is extending FragmentStatePagerAdapter, and its default behaviour is to preload at least one page for optimisation reasons. You can set the number of fragments it preloads using setOffscreenPageLimit but it has to be at least one.

You can use this method in your fragment and put in there something that you want executed only once the fragment becomes visible:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {

    }
}
Suleyman
  • 2,765
  • 2
  • 18
  • 31
  • @Ezio using this method, it's a callback, this method will only be called once the fragment is visible. So you have your 3 fragments, you are on a fragment B, all the code from the fragment C will be executed, but if you put some code in the `setUserVisibleHint` it will only be executed when the fragment actually appears on the screen. – Suleyman May 07 '18 at 13:43
  • thank you soo much you are genuis , +1 million if i can , it just a new account – Dante May 07 '18 at 14:03
  • @Ezio you are welcome :) you can mark the answer as accepted by clicking on the check mark beside it :) – Suleyman May 07 '18 at 14:09
  • although when i use this callback in the first fragment that is loaded none of the instruction gets executed (instead it gives me errors ) – Dante May 07 '18 at 14:39
  • @Ezio I found this post that was addressing the [exact problem](https://stackoverflow.com/a/25822814/7915814) you have, try using `viewPager.addOnPageChangedListener` instead – Suleyman May 07 '18 at 14:42
  • thanks again , but i used if (isAdded() && isVisible() && getUserVisibleHint()) inside setuservisibility() and it works just fine – Dante May 07 '18 at 15:10
  • @Ezio your choice, but read the post I linked, it seems like a better choice :) good luck! – Suleyman May 07 '18 at 15:18