How to unlink simultaneous reading of class codes in fragments in Android JAVA

Asked

Viewed 13 times

0

I am creating a java application using fragments but noticed that when the program opens the first fragment, it runs the codes of all java classes of the fragments simultaneously, that is, when the app opens the first fragment, it rotates the corresponding activity and also rotates all other activities of other fragments that have not been opened and this is preventing me from achieving my goal.

Here’s my main class of fragments:

public class Fragments extends Appcompatactivity { private viewpager viewPager; public Controller Controller; private tablayout tabLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragmentos);

    tabLayout = findViewById(R.id.tabLayout);
    viewPager = findViewById(R.id.viewpager);
    controlador = new Controlador(getSupportFragmentManager(), tabLayout.getTabCount());

    viewPager.setAdapter(controlador);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            if(tab.getPosition()==0){
                controlador.notifyDataSetChanged();
                viewPager.setCurrentItem(tab.getPosition());
            }
            else if(tab.getPosition()==1){
                controlador.notifyDataSetChanged();
                viewPager.setCurrentItem(tab.getPosition());
            }
            else if(tab.getPosition()==2){
                controlador.notifyDataSetChanged();
                viewPager.setCurrentItem(tab.getPosition());
            }
            else if(tab.getPosition()==3){
                controlador.notifyDataSetChanged();
                viewPager.setCurrentItem(tab.getPosition());
            }
        }

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

        }

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

        }
    });
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setVisibility(View.GONE);
}

Here is my Pageadapter class

public class Controller extends Fragmentpageradapter {

private int numerodeTab;
public Controlador(@NonNull FragmentManager fm, int numeroDeTab){
    super(fm);
    this.numerodeTab=numeroDeTab;
}

@NonNull
@Override
public Fragment getItem(int position) {




    switch (position){
        case 0:
            return new meses();
        case 1:
            return new valor();
        case 2:
            return new Positividade();
        case 3:
            return new Descricao();
        default:
            return null;
    }

}

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

}

If anyone needs more details about my code just ask.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.