Reputation: 348
How can I disable reloading fragment when clicking again on the current tab in BottomNavigationView
?
In other words how to disable re-selection in BottomNavigationView
?
PS: I'm using NavigationUI
to setup BottomNavigationView
Upvotes: 2
Views: 1631
Reputation:
For anyone looking for an up-to-date implementation in Kotlin:
NavigationUI.setupWithNavController(binding.bottomNavigationBar, navController)
binding.bottomNavigationBar.setOnItemReselectedListener {}
Upvotes: 2
Reputation: 838
In kotlin code looks like this:
val navView: BottomNavigationView = findViewById(R.id.nav_view)
navView.setOnNavigationItemReselectedListener {
// do nothing here when reselected
}
Upvotes: 1
Reputation: 348
It was surprisingly simple!
NavigationUI.setupWithNavController(bottomNavigationView, hostNavController);
bottomNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
// Nothing here to disable reselect
}
});
Upvotes: 9
Reputation: 1112
Try this and let me know Mohammad Hosein,
final Fragment fragment1 = new Fragment1();
final Fragment fragment2 = new Fragment2();
final Fragment fragment3 = new Fragment3();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1; //make the first screen as your active fragment.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container, fragment1, "1").commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.f1_menu_item:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
return true;
case R.id.f2_menu_item:
fm.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
return true;
case R.id.f3_menu_item:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
return true;
}
return false;
}
};
And in activity_main.xml,
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBF9F9"
tools:context=".view.HomeScreenActivity"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:padding="1dp"
android:id="@+id/main_container">
</FrameLayout>
</RelativeLayout>
let me know if you stuck anywhere Hosein. And accept the answer if this works.
Upvotes: 1