0

I have a tab layout with 3 tab items each having its own fragment to load at runtime when selected.

MainActivity.class: Here call to LoadFragment according to position of selected tab.

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        LoadFragment(new LocationFragment());
                        break;
                    case 1:
                        LoadFragment(new InformationFragment());
                        break;
                    case 2:
                        LoadFragment(new CommentsFragment());
                        break;
                }
            }

My question is how can I use switch statement for the logic to load proper fragment using fragment manager? I don't want to have if else statement hierarchy.

private void LoadFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        switch (*some logic here*) {

            case fragment instanceof LocationFragment:
                Log.i("MainActivity", "inside load fragment");
                ft.replace(R.id.coordinate_layout, new LocationFragment());
                break;


            case fragment instanceof InformationFragment:
                ft.replace(R.id.coordinate_layout, new InformationFragment());
                break;


            case fragment instanceof CommentsFragment:
                ft.replace(R.id.coordinate_layout, new CommentsFragment());
                break;

            ft.commit();

        }
    }

1 Answers1

0

You Don't need to create instances multiple times, you can simply pass the type and while loading you can decide which fragment you need to load.

    private static final int TYPE_LOCATION=0;
    private static final int TYPE_INFO=1;
    private static final int TYPE_COMMENT=2;

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        LoadFragment(TYPE_LOCATION);
                        break;
                    case 1:
                        LoadFragment(TYPE_INFO);
                        break;
                    case 2:
                        LoadFragment(TYPE_COMMENT);
                        break;
                }
            }

private void LoadFragment(int typeOfFragment) {
        Log.i("MainActivity", "inside load fragment");
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        switch (typeOfFragment) {

            case TYPE_LOCATION:   
              ft.replace(R.id.coordinate_layout, new LocationFragment());
                break;


            case TYPE_INFO:
                ft.replace(R.id.coordinate_layout, new InformationFragment());
                break;


            case TYPE_COMMENT:
                ft.replace(R.id.coordinate_layout, new CommentsFragment());
                break;

            ft.commit();

        }
    }
Alok
  • 881
  • 1
  • 11
  • 18