-1

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"/>

Rahim T.S
  • 13
  • 5
  • can you add your code and also crash reports. Just by switching tab, it should not crash. – Abdul Apr 23 '20 at 07:09
  • Could you explain your problem more specifically? What is your 'Bottombar', is it a layout, BottomNavigationView, ActionBar? And what exactly causes the crash? Could you post some relevant code or a minimal, reproducible example: https://stackoverflow.com/help/minimal-reproducible-example – Jorn Rigter Apr 23 '20 at 07:09
  • it's actually BottomNavigationView, – Rahim T.S Apr 23 '20 at 07:36
  • when I try to switch fragments, it causes NullPointerException. But no problems occur if I switch fragments after the test is completed – Rahim T.S Apr 23 '20 at 07:38

3 Answers3

1

You can disable bottomNavigationBar like below:

bottomNavigationBar.setEnabled(false);

or you can also hide bottomNavigationBar like below:

bottomNavigationBar.setVisibility(View.GONE);
Bhoomi Vaghasiya
  • 311
  • 1
  • 5
  • 12
0

Just had to declare

 BottomNavigationView bottomNav; 

this as a global variable

and put

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

inside OncreateView()

Rahim T.S
  • 13
  • 5
0

You need to disable each menu item separately:

    for (i in 0 until bottomNavigationView.menu.size()) {
        mainBinding.bottomNavigationView.menu.getItem(i).isEnabled = false
    }
Commanche
  • 218
  • 1
  • 3
  • 11