I have a BottomNavigationView (and the latest JetPack Navigation Component) created in MainActivity that I need to show/hide in different Fragments according to the authentication state of the user.
Up to just a few days ago, I would just retrieve it from the Fragment with requireActivity().findViewById(R.id.nav_view) (not the best practice, I realize, but it was acceptable for now).
After upgrading to Android Studio Bumblebee along with a few more Navigation libraries, that gives NullPointerException.
I then tried by using an Interface like below but still getting NPE.
I would really want to still create the bottom bar in one place only and be able to retrieve it from anywhere accordingly.
main_activity.xml
<androidx.constraintlayout.widget.ConstraintLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements HomeFragment.onGetBottomNavViewListener {
private ActivityMainBinding binding;
private BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
bottomNavigationView = binding.navView;
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
@Override
public void showBottomNavView() {
bottomNavigationView.setVisibility(View.VISIBLE);
}
@Override
public void hideBottomNavView() {
bottomNavigationView.setVisibility(View.INVISIBLE);
}
}
HomeFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/home_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.home.HomeFragment"
android:paddingStart="@dimen/activity_padding"
android:paddingTop="@dimen/activity_padding"
android:paddingEnd="@dimen/activity_padding"
android:paddingBottom="100dp">
<LinearLayout>....</LinearLayout>
<GridLayout>....</GridLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/search_btn"
style="@style/BaseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginBottom="5dp"
android:text="Go!"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/gridLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
HomeFragment.java
public class HomeFragment extends Fragment implements View.OnClickListener {
private onGetBottomNavViewListener listener;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
user = FirebaseAuth.getInstance().getCurrentUser();
View root = binding.getRoot();
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((AppCompatActivity) requireActivity()).getSupportActionBar().setSubtitle("");
// If user is logged in
if (user != null) {
listener.showBottomNavView();
} else {
listener.hideBottomNavView();
}
}
// INTERFACE
public interface onGetBottomNavViewListener {
void showBottomNavView();
void hideBottomNavView();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
// Store the listener (activity) that will have events fired once the fragment is attached
if (context instanceof onGetBottomNavViewListener) {
listener = (onGetBottomNavViewListener) context;
} else {
throw new ClassCastException(context +
"must implement HomeFragment.onGetBottomNavViewListener");
}
}
}
UPDATE: still can't get the bottomBar from the fragment but I solved this particular case by simply moving the authentication check, along with the bar managing, in the MainActivity with
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
navView.setVisibility(View.GONE);
} else {
navView.setVisibility(View.VISIBLE);
}
}
});
You could create a method for Showing and hiding the BottomNavugationView in your activity. In your fragment would just call
or