0

I have an Android app (Java) which uses the Navigation Component to set up a Bottom Navigation. The app consists of a single Activity (Main), everything else is loaded in fragments.

The idea is for the Splash Screen to launch and check if the user is logged in or not. If the user is logged in, then the home screen loads and the bottom navigation (previously hidden) becomes visible.

The bottom navigation consists of 3 tabs.

However, when the user signs in, it can be one of two types of users. If he's a subscriber he will get access to all sections of the app. However if the user is a visitor, he will get access to only two sections. In this case I want to have a different bottom navigation menu to be visible with only 2 tabs.

How can I replace the bottom navigation depending on the user type if the main bottom navigation has already been assigned in the main activity?

This is the code in the MainActivity:

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    FragmentManager supportFragmentManager = getSupportFragmentManager();
    NavHostFragment navHostFragment = (NavHostFragment) supportFragmentManager.findFragmentById(R.id.nav_host_fragment);
    NavController navController = navHostFragment.getNavController();

    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.homeFragment, R.id.libraryFragment, R.id.searchFragment, R.id.myStuffFragment).build();
    NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);

    BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
    NavigationUI.setupWithNavController(bottomNav, navController);


    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
            if (destination.getId() == R.id.splashScreenFragment || destination.getId() == R.id.welcomeFragment || destination.getId() == R.id.signInFragment) {
                toolbar.setVisibility(View.GONE);
                bottomNav.setVisibility(View.GONE);
            } else if (destination.getId() == R.id.audioPlayerFragment) {
                bottomNav.setVisibility(View.GONE);
            } else {
                toolbar.setVisibility(View.VISIBLE);
                bottomNav.setVisibility(View.VISIBLE);
            }
        }
    });

This is the code in the SplashScreenFragment (this is the startDestination in the nav_graph):

public class SplashScreenFragment extends Fragment {

private static final String TAG = "SplashScreenFragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_splash_screen, container, false);

    return v;
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    SharedPreferences getSharedData = this.getActivity().getSharedPreferences("AppTitle", Context.MODE_PRIVATE);
    Boolean loggedIn = getSharedData.getBoolean("isUserLoggedIn", false);


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if (!loggedIn) {
                Navigation.findNavController(view).navigate(R.id.action_splashScreenFragment_to_welcomeFragment);
            } else {
                Navigation.findNavController(view).navigate(R.id.action_splashScreenFragment_to_homeFragment);
            }
        }
    }, 2000);
}
cesarcarlos
  • 1,271
  • 1
  • 13
  • 33
  • So what is your question? How you can change what menu items are visible in your `BottomNavigationView`? That doesn't have anything to do with Navigation, that's just using the `Menu` APIs. – ianhanniballake Jun 01 '21 at 00:46
  • @ianhanniballake if the user is not a subscriber the bottom menu will be completely different from the subscriber bottom menu. So how do I work with the Navigation Component and two different bottom menus depending on user type? – cesarcarlos Jun 01 '21 at 00:51
  • Navigation doesn't have anything to do with what menu you inflate into your `BottomNavigationView` - that's why that `menu.xml` is completely independent of your navigation graph XML. Are you asking how to inflate a different menu for your `BottomNavigationView` based on what type of user? – ianhanniballake Jun 01 '21 at 01:02

1 Answers1

0

You can have the full menu items by default in the BottomNavigationView, so when a subscribed user logged in, then no change in the menu is required.

And if the user is not subscribed, then you can remove the needed items from the menu programmatically using removeItem()

So, in MainActivity add a condition :

if (notSubscribedUser) {
    if (bottomNav.getMenu().findItem(R.id.my_item_id) != null)
        bottomNavigationView.getMenu().removeItem(R.id.my_item_id);
}

And do the same for all menu item ids that you want to remove

Zain
  • 37,492
  • 7
  • 60
  • 84
  • How can I call the MainActivity from the SplashScreenFragment which is handling the condition of subscribed vs non-subscribed? – cesarcarlos Jun 01 '21 at 01:16
  • You can call `requireActivity()` in `SplashScreenFragment` and cast it to MainActivity .. like `MainActivity activity = (MainActivity) requireActivity()` then put the code above in some method for instance `checkSubscription` and call it with `activity.checkSubscription()` – Zain Jun 01 '21 at 01:24