Issue
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);
}
Solution
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
Answered By - Zain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.