1

guys please I'm working on an android project and I need to pass data from a parent activity that is hosting a BottomNavigationView to its child fragment. I've tried to follow some solutions online but seems my issues is peculiar because it involves a BottomNavigationView

RhemaHiveMessagingClientActivity.java

Inside my onCreate()

try{
            messageBundle = new Bundle();
            rhemFrag = new RhemaHiveMessageFragment();
            messageBundle.putString("user_church", churchName);
            rhemFrag.setArguments(messageBundle);
            getAuto().getToast(c, "Church Passed is " + churchName, RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();

        }catch(NullPointerException np){
            getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
        }

RhemaHiveMessageFragment.java

in my onCreateView()

 churchBund = getArguments();
            if(churchBund.containsKey("user_church")){
                churchName = churchBund.getString("user_church");
                getAuto().getToast(getContext(),"Church Recieved : " + churchName, RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
            }
            else{
                getAuto().getToast(getContext(),RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + " unable to retrieve Church",RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
            }

This doesn't return any data in my fragment

Also one more thing, this is the code that I use to populate the BottomNavigationView in my parent activity

aBar  = getSupportActionBar();
            aBar.setTitle("Rhema Social");
            loadFragment(new RhemaHiveMessageFragment());


            bottomNavigationView = findViewById(R.id.bottom_nav);
            layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigationView.getLayoutParams();
            layoutParams.setBehavior(new RhemaHiveBottomNavBehaviorClass());
            bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    try {
                        switch (item.getItemId()) {
                            case R.id.rhem_user_messaging:
                                aBar.setTitle(getString(R.string.message));
                                loadFragment(new RhemaHiveMessageFragment());
                                return true;
                            case R.id.rhem_user_forum:
                                aBar.setTitle(getString(R.string.forums_lab));
                                loadFragment(new RhemaHiveGroupMessageFragment());
                                return true;
                            case R.id.rhem_user_starred:
                                aBar.setTitle(getString(R.string.starred_chats));
                                loadFragment(new RhemaHiveStarredChatsFragment());
                                return true;
                            case R.id.rhem_user_add_friend:
                                aBar.setTitle(getString(R.string.find_hivers));
                                loadFragment(new RhemaHiveFindHiverFragment());
                                return true;
                        }
                    }
                    catch(NullPointerException np){
                        getAuto().getToast(c,RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                    }
                        return false;

                }
            });






        }catch(NullPointerException np){
            getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
        }




    }






    private void loadFragment(Fragment fragment){
        FragmentTransaction transaction =  getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.rhema_mess_cont,fragment);
        transaction.addToBackStack(null);
        transaction.commit();

    }
MustafaKhaled
  • 1,343
  • 9
  • 25
Uncle Sam
  • 61
  • 2
  • 10

3 Answers3

0

You always create new Fragment inside onNavigationItemSelected and don't put any data to Fragment. Change your code like this.

switch (item.getItemId()) {
  case R.id.rhem_user_messaging:
    aBar.setTitle(getString(R.string.message));
    messageBundle = new Bundle();
    rhemFrag = new RhemaHiveMessageFragment();
    messageBundle.putString("user_church", churchName);
    rhemFrag.setArguments(messageBundle);
    loadFragment(rhemFrag);
    return true;
  case R.id.rhem_user_forum:
    aBar.setTitle(getString(R.string.forums_lab));
    loadFragment(new RhemaHiveGroupMessageFragment());
    return true;
  case R.id.rhem_user_starred:
    aBar.setTitle(getString(R.string.starred_chats));
    loadFragment(new RhemaHiveStarredChatsFragment());
    return true;
  case R.id.rhem_user_add_friend:
    aBar.setTitle(getString(R.string.find_hivers));
    loadFragment(new RhemaHiveFindHiverFragment());
    return true;

Or if you already created RhemaHiveMessageFragment in onCreate method of your Activity, just use this fragment instead of re-create it like this.

switch (item.getItemId()) {
      case R.id.rhem_user_messaging:
        aBar.setTitle(getString(R.string.message));
        loadFragment(rhemFrag);
        return true;
      case R.id.rhem_user_forum:
        // ...rest of the code
  • Hi Eugene, I've done this and still it doesn't receive the data in the fragment – Uncle Sam Apr 08 '20 at 13:50
  • Okay guys..Thanks Eugune, Ive resolved the issue.. I just realized i used the wrong bundle name when sending the data..I changed to the right one and it worked perfectly – Uncle Sam Apr 08 '20 at 18:52
0

When you are setting fragment for bottomnavigation you creating new object for that fragment. So, it's not adding argumaents in that. So, please use RhemaHiveMessageFragment fragment variable which you create in onCreateView

      bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                try {
                    switch (item.getItemId()) {
                        case R.id.rhem_user_messaging:
                            aBar.setTitle(getString(R.string.message));
                            loadFragment(rhemFrag);
                            return true;

                        // other code will same                                  
                    }
                }
                catch(NullPointerException np){
                    getAuto().getToast(c,RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                }
                    return false;

            }
        });

    }
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
0

You should pass new instance of RhemaHiveMessageFragment that you have created in onCreate.

switch (item.getItemId()) {
     case R.id.rhem_user_messaging:
         aBar.setTitle(getString(R.string.message));
         loadFragment(rhemFrag);
     ...
}
Amitoz singh
  • 144
  • 1
  • 7