0

I'm having some trouble retrieving arguments in a Fragment using BottomNavigation and NavigationUI. I retrieve null value after navigating to an addOrderFragment(picture). I'm out of ideas why this doesn't work.

Navigation layout: http://prntscr.com/pf9tl1
Activity layout: http://prntscr.com/pf9u4q
Activity layout has a NavHost fragment in which all other layouts are inflated and navigation in this segment works fine.

Main activity

  UID = getIntent().getStringExtra("UID");
    Bundle args = new Bundle();
    args.putString("UID", UID);
    //navigation
    controller = Navigation.findNavController(this, R.id.fragment_container);
    controller.setGraph(R.navigation.navigation_mainmenu, args);
    controller.navigate(R.id.mainMenuFragment, args);
    bottomNav = findViewById(R.id.bottom_navigation);
    NavigationUI.setupWithNavController(bottomNav, controller);

Retrieving UID from another fragment after being navigated to it

 controller = Navigation.findNavController(view);
        try {
            UID = getArguments().getString("UID"); <---gets null 
        } catch (Exception e) {
            Crashlytics.logException(e);
        }
xblaz3kx
  • 341
  • 1
  • 3
  • 15
  • If your UID is global to your activity, why are you using Fragment arguments at all? Fragments can call `requireActivity().getIntent().getStringExtra("UID")` itself – ianhanniballake Oct 05 '19 at 18:11
  • @ianhanniballake I'm not sure why tbh. It works now, thank you. – xblaz3kx Oct 05 '19 at 19:49

1 Answers1

0

If your UID is global to your activity, you shouldn't be using Fragment arguments at all. Fragments can call requireActivity().getIntent().getStringExtra("UID") to retrieve extras from the Activity.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443