• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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

sariDon2

Lurker
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");
topLevelDestinations.add(R.id.nav_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"));
topLevelDestinations.add(R.id.nav_fragment);
} catch (JSONException e) {
e.printStackTrace();
}

}

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.
 
Back
Top Bottom