Switch between Fragments in BottomNavigationView

fragment re-created on bottom navigation view item selected
bottom navigation view with fragments (no fragment re-creation).
saving fragment states with bottomnavigationview
android bottom navigation save fragment state
hide bottom navigation bar in fragment
bottomnavigationview change fragment programmatically
refresh fragment in bottom navigation android
android avoid fragment recreation

I'm working with a simple app with Bottom Navigation View. I have 3 fragments (layout and java). I have BottonNavigationView, declared in my MainActivity.java. My bottonnavigation have 3 items, for the 3 fragments. So, in my MainActivity.java, when i select a item, it start one fragment. So, when i select again another item, nothing happens, because in the java fragment i need to declare the BottonNavigationView, but i don't know how to set it to switch the actual fragment with another fragment. I tried this link, but no success: https://developer.android.com/training/basics/fragments/fragment-ui.html

Sorry my bad english

Here the codes:

Main Activity

 @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Fragment selectedFragment = null;
    switch (item.getItemId()) {
        case R.id.navigation_home:
            selectedFragment = HomeFragment.newInstance();
            break;
        case R.id.navigation_dashboard:
            selectedFragment = DashboardFragment.newInstance();
            break;
        case R.id.navigation_notifications:
            selectedFragment = NotificationsFragment.newInstance();
            break;
    }
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.content, selectedFragment);
    transaction.commit();
    return true;
}

Fragment Java Example

public class HomeFragment extends Fragment {
public static HomeFragment newInstance() {
HomeFragment fragment = new HomeFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.navigation_home, container, false);
return inflater.inflate(R.layout.navigation_home, container, false);
}

You can try it:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Fragment selectedFragment = null;
    switch (item.getItemId()) {
        case R.id.navigation_home:
            selectedFragment = HomeFragment.newInstance();
            break;
        case R.id.navigation_dashboard:
            selectedFragment = DashboardFragment.newInstance();
            break;
        case R.id.navigation_notifications:
            selectedFragment = NotificationsFragment.newInstance();
            break;
    }
    getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
    return true;
}

Bottom Navigation View With Fragments (No fragment re-creation)., Using fragments was cool until i tried applying it to bottom navigation To move between activities, i customized the BottomNavigationView. In this video you will learn, how to add a bottom navigation to your activity and use it to switch between different fragments. We will fill our BottomNavigationView with 3 menu items and then

You don't have to create newInstance every time. you can save the fragment states. follow the below link

fragmentManager.beginTransaction().hide(toBeHidden).show(toBeShown).commit();

https://medium.com/@oluwabukunmi.aluko/bottom-navigation-view-with-fragments-a074bfd08711

BottomNavigationView with Fragments, In this video you will learn, how to add a bottom navigation to your activity and use it to switch Duration: 16:07 Posted: Feb 24, 2018 My bottonnavigation have 3 itens, for the 3 fragments. So, in my MainActivity.java, when i select a item, it start one fragment. So, when i select again another item, nothen happem, because in the java fragment i nedd to declare the BottonNavigationView, but i don't know how to set it to switch the actual fragment with another fragment.

In this case is better to try get exist fragment from fragmentManager like fragmentManager.findFragmentByTag(tag). You can switch more smoothly and you don't need for example load some content from network (if you have such code in fragment or presenter of fragment)

Bottom Navigation Android Example using Fragments, How do I stop refreshing fragments on tab is changed? Bottom navigation should be used when an application has three to five top-level destinations. Using the bottom navigation user can easily switch between top-level views in a single tap. In this example, we have used BottomNavigationView with Fragments.

You should create once newIstance from each your Fragments. and later you can hide active fragment and then show new fragment.

    Fragment activeFragment;
    ArrayList<Fragment> fragment;

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment = null;
            switch (item.getItemId()) {
                case R.id.navigation_fragment:
                    selectedFragment = ShowMyFragment.newInstance();
                    replaceFragment(selectedFragment);
                    return true;
}
});
    private void replaceFragment(Fragment selectedFragment) {
        boolean lastOpened = false;
        for ( int i=0; i<fragment.size();i++ )
        {
            if ( fragment.get(i) == selectedFragment ) {
                lastOpened = true;
                break;
            }
        }
        if (!lastOpened) {
            getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
        }
        else
        {
            getSupportFragmentManager().beginTransaction().hide(activeFragment).show(selectedFragment).commit();
        }

        activeFragment = selectedFragment;
    }

How to Code a Bottom Navigation Bar for an Android App, How do I add a bottom navigation bar in fragment? Don’t forget to change the layout resource id (R.layout.file_name) with the layout that you want to display for the fragment. Switching Fragments. Now we will switch the screens or fragments when the bottom navigation menu is clicked. We also need to load some fragment initially which is HomeFragment in this case.

Just to add a blank implementation like this in onCreate bottomNavView.setOnNavigationItemReselectedListener {}

Saving Fragment States with BottomNavigationView, How do you handle bottom navigation perfectly with back press? The way I would do it is, I would first add three methods similar to this one (each for a single fragment. Replace the layout name and the fragment object to the appropriate fragment that is being switched to): public void switchToFragment1() { FragmentManager manager = getSupportFragmentManager(); manager.beginTransaction().replace(R.id.your_fragment_layout_name, new Fragment1()).commit(); }

Saving Fragment States with BottomNavigationView, icon takes you directly to the associated view or refreshes the currently active view. To move between activities, i customized the BottomNavigationView.OnNavigationItemSelectedListener method to hide the active fragment, commit it, then set the current fragment as the active

Android BottomNavigationView with fragments, Each of the tabs could have a stack of fragments shown, and user could swap between them. With this, we hope when we tap across the bottom  Same way create Fragment class for second Fragment and set the layout. Now If you want to switch your fragment layout in Fragment1 to another layout on click of a button then create another class say Fragment3.java and set the layout you want to switch and write the below code inside the Fragment1.java

BottomNavigationView, In this blog, we are going to understand how to save the fragment states the user switches between fragments using BottomNavigationView. Bottom navigation provide easy navigation for switching between 3 to 5 different views. It was recently added to the Material Design spec, which provides an alternative between tabs and navigation drawers. This widget was also added to v25 of the support design library. Setup

Comments
  • why you always create new instance?
  • in my main_activity layout, i have a linearlayout with FrameLayout and BottomNavigationView, i think i need to create a new instance, no ?
  • you can create fragments just once, and you can use them again.
  • Can you show me a code ? and how i will switch between fragments on item selected ?
  • For example, Fragment homeFragment = HomeFragment.newInstance(); Fragment notificationsFragment = NotificationsFragment.newInstance(); And in switch case, you can set selectedFragment = homeFragment or something else
  • Wow man, looks like is working, thank you. Just one more question: i have three fragments (Home, Dashboard and Notifications). The Home fragment is the principal menu of my app. So, if i am in Dashboard or Notifications, and i press the button, i want to set the onKeyDown to back for Home Fragment, and, in home fragment, if i press back button, i want to close de app. ou know how to do that ?
  • If you want transition between Fragments, call addToBackStack(). stackoverflow.com/questions/7992216/…
  • Thanks @Thien, is a lot of codes but i will try them. Thanks
  • @ThienHuynh how do you set the fragment initially on the first run?
  • @RazorHead navigationView.getMenu().getItem(0).setChecked(true); With item 0 it mean HomeFragment is the first.
  • Not working if you change toolbar in fragments. After screen rotation toolbar menus are overlaying