Search code examples
javaandroidandroid-fragmentsbottomnavigationviewbottombar

How to disable BottomNavigationView while something is running in the selected Fragment?


I make use of BottomNavigationView in the current project I do. I have four fragments. This is a SpeedTest app. If I try to switch to another Fragment while the speed test is running, the app crashes. What I think is to disable the Bottombar while it's running. But I don't know how to do it. Is there any way to do this?

This is the Main Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle  savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);

    if (savedInstanceState == null){
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new SpeedFragment()).commit();
    }

}


 private BottomNavigationView.OnNavigationItemSelectedListener navListener = new
       BottomNavigationView.OnNavigationItemSelectedListener() {
           @Override
           public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                    switch (item.getItemId()){
                    case R.id.nav_speed:
                        selectedFragment=new SpeedFragment();
                        break;
                    case R.id.nav_web:
                        selectedFragment=new WebFragment();
                        break;
                    case R.id.nav_video_test:
                        selectedFragment=new VideoFragment();
                        break;
                    case R.id.nav_history:
                        selectedFragment=new HistoryFragment();
                        break;
                }
               assert selectedFragment != null;
               getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
               return true;
           }
       };

}

This is activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom_navigation"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:menu="@menu/bottom_navigation"
    android:background="?android:attr/windowBackground"
    app:labelVisibilityMode="labeled"/>


Solution

  • Just had to declare

     BottomNavigationView bottomNav; 
    

    this as a global variable

    and put

     bottomNav = getActivity().findViewById(R.id.bottom_navigation); 
    

    inside OncreateView()