1. Are you ready for the Galaxy S20? Here is everything we know so far!

Pass bundle in Android (with drawer layout) destination(HashSet) via 'NavController' and 'AppBarConf

Discussion in 'Android Development' started by sariDon2, Jul 9, 2020.

  1. sariDon2

    sariDon2 Lurker
    Thread Starter

    I am an amateur in Android Development. I have started working with a Drawer layout and want to populate the drawer navigational links dynamically (from a JSON variable say vtxJSON). All the links will direct to the same Fragment (say R.id.nav_fragment) with separate Bundles to be passed for each link to the same fragment.

    Till now I have created the Menu and directed the links successfully to the Fragment R.id.nav_fragment (with much appreciated help from this StackOverflow answer). Now I want to pass a set of values (preferably in Bundles) on each click and process them in the Fragment class and its View Model class.

    RE: I am using androidx libraries.

    My code is like this (MainActivity.java):

    Set<Integer> topLevelDestinations = new HashSet<>();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    final Menu menu = navigationView.getMenu();
    menu.add(R.id.act_drawer_group, R.id.nav_home, 0, "HOME");
    int menulen= vtxJSON.length();
    for(int i=0; i< menulen; i++)
    try {
    JSONObject ttxobj= (JSONObject) vtxJSON.get(i);
    Bundle args = new Bundle();
    args.putString("vtx_name", (String) ttxobj.get("name"));
    args.putString("vtx_mail", (String) ttxobj.get("mail"));
    menu.add(R.id.act_drawer_group, R.id.nav_fragment, i+1, (String) ttaxobj.get("name"));
    } catch (JSONException e) {


    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    AppBarConfiguration pxAppBar = new AppBarConfiguration.Builder(topLevelDestinations).setDrawerLayout(drawer).build();
    NavigationUI.setupActionBarWithNavController((AppCompatActivity) this, navController, pxAppBar);
    NavigationUI.setupWithNavController(navigationView, navController);

    Please at least let me know if I am on the right path. A hint or an answer would be wonderful. Thanks.


Share This Page