Home > Mobile >  TabLayout tabs getting destroyed on switching tabs
TabLayout tabs getting destroyed on switching tabs

Time:07-21

I am using tab layout in my activity which has two tabs. When i switch to another tab current tab fragment is destroyed, and another tab is created from scratch ( from Oncreate() lifecycle method) hoverver I went once before.

private void Tablayout_TabSelected(object sender, TabSelectedEventArgs e)
    {
        Tab tab = e.Tab;
        if (tab.Position == 0)
        {
            SupportFragmentManager.BeginTransaction().Replace(Resource.Id.fragmentContainerView, listFrag ??= new MusicListFragment(), "ListFrag").Commit();
        }
        else if (tab.Position == 1)
        {
            SupportFragmentManager.BeginTransaction().Replace(Resource.Id.fragmentContainerView, favFrag ??= new FavoriteSongsFragment(), "FavFrag").Commit();
        }
    }

is, isn't it a good idea to keep the whole fragment in memory instead of creating a new one every time?

Or How can i improve my switching ?

CodePudding user response:

Try this

Use FragmentPagerAdapter it is easy and work well method

  1. Create class extend FragmentPagerAdapter

  2. follow the below code in your adapter

    public class FragmentAdapter extends FragmentPagerAdapter {
        Context context;
        int totalTaps;
    
      public FragmentAdapter(@NonNull FragmentManager fm, Context context, int totalTaps) {
        super(fm);
        this.context = context;
        this.totalTaps = totalTaps;
    }
    
    @NonNull
    @Override
    public Fragment getItem(int position) {
    
        switch (position){
            case 0:
                MusicListFragment musicListFragment= new MusicListFragment();
                return musicListFragment;
            case 1:
                FavoriteSongsFragment favoriteSongsFragment= new FavoriteSongsFragment();
                return favoriteSongsFragment;
    
            default:
                return null;
        }
    }
    
     @Override
     public int getCount() {
        return totalTaps;
      }
    }
    

MainActivity

public class HomePage extends AppCompatActivity {

ViewPager viewPager;
TabLayout tabLayout;
FragmentAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);
    viewPager = findViewById(R.id.viewPager);
    tabLayout = findViewById(R.id.tabLayout);
    adapter = new FragmentAdapter(getSupportFragmentManager(),getApplicationContext(),2);

    tabLayout.addTab(tabLayout.newTab().setText("ListFrag"));
    tabLayout.addTab(tabLayout.newTab().setText("FavFrag"));

     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
     tabLayout.addOnTabSelectedListener(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) {

         }
     });
     viewPager.setAdapter(adapter);
}
  • Related