0

Following is my code for bottom navigation view item selected

public static void  setupBottomNavigationView(BottomNavigationViewEx bottomNavigationViewEx){
        Log.d(TAG, "setupBottomNavigationView: Setting up NavigationView1");
        bottomNavigationViewEx.enableAnimation(false);
        bottomNavigationViewEx.enableItemShiftingMode(false);
        bottomNavigationViewEx.enableShiftingMode(false);
        bottomNavigationViewEx.setTextVisibility(false);
    }

      public static void enableNavigation(final Context context, BottomNavigationViewEx view){
    view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment;
            switch (item.getItemId()){
                case R.id.nav_home:
                    fragment = new FragmentMarker();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_bookmark:
                    fragment = new FragmentBookmark();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_blog:
                    fragment = new FragmentBlog();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_notification:
                    fragment = new FragmentNotification();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_account:
                    fragment = new FragmentAccount();
                    loadFragment(fragment);
                    return true;

            }
           return false;
        }
    });
 }

Is there any way to display fragments on bottom navigation bar selected without re-creating activity?

1 Answers1

1
       @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment;
            switch (item.getItemId()) {
                case R.id.nav_home:
                    fragment = new HomeFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_bookmark:
                    fragment = new BookmarkFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_blog:
                    fragment = new BlogFragment();
                    loadFragment(fragment);
                    return true;

            }

            return false;
        }
    };

    private void loadFragment(Fragment fragment) {
        // load fragment
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
Nabil
  • 654
  • 7
  • 21
  • cannot resolve getSupportFragmentManager() and what is in frame_container? – Max Yablonskyi Jul 10 '18 at 09:20
  • About getSupportFragmentManager(), does your activity extends AppCompatActivity? Also, frame_container is a Frame inside your Activity. You can bind all your fragments to this frame_container. – Nabil Jul 10 '18 at 09:26
  • You can have a detailed look into bottom navigation view at: https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/ – Nabil Jul 10 '18 at 09:28
  • I have a method `enableNavigation` and it should be static. But `getSupportFragmentManager()` cannot be referenced from a static context – Max Yablonskyi Jul 10 '18 at 09:47
  • did you try passing getSupportFragmentManager() as a parameter to your function enableNavigation()? Please edit your question and add more necessary codes – Nabil Jul 10 '18 at 10:03
  • I edited my question. I am using this library https://github.com/ittianyu/BottomNavigationViewEx because i need to block animation in bottom navigation view – Max Yablonskyi Jul 10 '18 at 10:21
  • Change function `enableNavigation()` to `enableNavigation(final Context context, final BottomNavigationViewEx view, final FragmentManager supportFragmentManager)` and pass `getSupportFragmentManager()` as a parameter to `enableNavigation()` – Nabil Jul 10 '18 at 11:47
  • it did not help – Max Yablonskyi Jul 10 '18 at 12:48