Switch between Fragments in BottomNavigationView
Solution 1
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;
}
Solution 2
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
Solution 3
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)
Tony Starkus
Updated on June 30, 2022Comments
-
Tony Starkus almost 2 years
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); }
-
Tony Starkus almost 7 yearsWow 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 ?
-
Thien Huynh almost 7 yearsIf you want transition between Fragments, call addToBackStack(). stackoverflow.com/questions/7992216/…
-
Tony Starkus almost 7 yearsThanks @Thien, is a lot of codes but i will try them. Thanks
-
RazorHead over 6 years@ThienHuynh how do you set the fragment initially on the first run?
-
Thien Huynh over 6 years@RazorHead navigationView.getMenu().getItem(0).setChecked(true); With item 0 it mean HomeFragment is the first.
-
RazorHead about 6 years@ThienHuynh sorry but I meant to ask how do you load the initial fragment the first time activity starts. your suggestion activates the bottom nav.
-
Neuron over 4 yearsNot working if you change toolbar in fragments. After screen rotation toolbar menus are overlaying
-
Syed Ahmed Jamil over 4 yearsWait how is this any different than the OP mentioned in his question ? Am I missing something ? I can't see any difference.