0

I have a simple nav drawer with a bunch of fragments. I want it to display different home fragments based on the type of account a user logs into(and hide the other/replace it). How could I achieve this? Any help is appreciated.

This is just my default NavActivity, haven't changed much of anything in it -

public class NavActivity extends AppCompatActivity {
    private TextView nav_profile;
    private TextView nav_email;
    private AppBarConfiguration mAppBarConfiguration;
    private ActivityNavBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityNavBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.appBarNav.toolbar);
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_about, R.id.nav_contact, R.id.nav_compost)
                .setOpenableLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_nav);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        View headerView = navigationView.getHeaderView(0);
        nav_profile = headerView.findViewById(R.id.nav_profile_id);
        nav_email = headerView.findViewById(R.id.nav_profile_email);
        try {
            nav_profile.setText(DBHelper.checkuser);
            nav_email.setText(DBHelper.checkemail);
        }catch(NullPointerException e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.nav, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.action_settings:
                Intent intent = new Intent(this, Settings_Activity.class);
                startActivity(intent);
                return true;
        }
        return false;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_nav);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}
Sai Nishwanth
  • 111
  • 1
  • 8
  • Found the answer [here](https://stackoverflow.com/questions/35402018/replace-viewpager-fragment-based-on-particular-condition) Any Additional tips are appreciated. – Sai Nishwanth May 26 '22 at 10:32

0 Answers0