Marvin
Marvin

Reputation: 11

Multiple fragments in one tab of bottom navigation

In my app i have 5 fragments. I use bottom navigation to manage them. But in first fragment i have recyclerview and when i click on recycler item i need to open second fragment in this tab (first tab).

public class ActivityBottom extends AppCompatActivity {

    final Fragment fragment1 = new FragmentMarker();
    final Fragment fragment2 = new FragmentBookmark();
    final Fragment fragment3 = new FragmentMap();
    final Fragment fragment4 = new FragmentNotification();
    final Fragment fragment5 = new FragmentAccount();
    final FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment active = fragment1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        BottomHelper.disableShiftMode(bottomNavigationView);

        fragmentManager.beginTransaction().add(R.id.frame_container, fragment5, "5").hide(fragment5).commit();
        fragmentManager.beginTransaction().add(R.id.frame_container, fragment4, "4").hide(fragment4).commit();
        fragmentManager.beginTransaction().add(R.id.frame_container, fragment3, "3").hide(fragment3).commit();
        fragmentManager.beginTransaction().add(R.id.frame_container, fragment2, "2").hide(fragment2).commit();
        fragmentManager.beginTransaction().add(R.id.frame_container, fragment1, "1").commit();
    }


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment = null;
            switch (item.getItemId()) {
                case R.id.nav_home:
                    fragmentManager.beginTransaction().hide(active).show(fragment1).commit();
                    active = fragment1;
                    return true;
                case R.id.nav_bookmark:
                    fragmentManager.beginTransaction().hide(active).show(fragment2).commit();
                    active = fragment2;
                    return true;
                case R.id.nav_blog:
                    fragmentManager.beginTransaction().hide(active).show(fragment3).commit();
                    active = fragment3;
                    return true;
                case R.id.nav_notification:
                    fragmentManager.beginTransaction().hide(active).show(fragment4).commit();
                    active = fragment4;
                    return true;
                case R.id.nav_account:
                    fragmentManager.beginTransaction().hide(active).show(fragment5).commit();
                    active = fragment5;
                    return true;
            }
          return false;
        }
    };
}

Right now when i am trying to open second fragment he opens above all tabs.

This is how do I open a second fragment

@Override
        public void onItemClick(Marker marker) {
            MarkerDetailsFragment markerDetailsFragment = new MarkerDetailsFragment();
            Bundle bundle = new Bundle();
            bundle.putParcelable("marker", marker);
            markerDetailsFragment.setArguments(bundle);
            getActivity().getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_container, markerDetailsFragment, "MarkerDetailsFragment")
                    .addToBackStack(null)
                    .commit();
        }

Upvotes: 0

Views: 1396

Answers (1)

Dmytro Ivanov
Dmytro Ivanov

Reputation: 1310

Probably you want to make something like in the Instagram. All your containers should have their own stack history.

First, create the host activity which will contain bottom navigation menu, and containers.

Next, create the fragment containers, if you have 5 menu items, create 5 containers, inside them, you could create own navigation logic. Then if user clicks a menu item, you should detach the active container, and attach the selected container. Below, screenshot with details.

enter image description here

Upvotes: 1

Related Questions