0

I'm trying to replace from fragment to fragment, but it replaces without changing selected item in BottomNavigation. Even i searched a lot I wasn't able to find the way. Can anybody help?

Here is code:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_upload, container, false);
       BottomNavigationView bottom =inflater.inflate(R.layout.activity_main,container,false).findViewById(R.id.mainactivity_bottom_navi);


FirebaseDatabase.getInstance().getReference().child("posts").child(postModel.writerUid)
                                        .push().setValue(postModel)
                                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                                            @Override
                                            public void onSuccess(Void aVoid) {

                                                Toast.makeText(getActivity(), "Uploaded Successfully!", Toast.LENGTH_SHORT).show();

                                /*those are what i tried*/

                                                bottom.getMenu().findItem(R.id.action_home).setChecked(true);
                                                //bottom.setSelectedItemId(R.id.action_home);
                                                //View view = bottom.findViewById(R.id.action_home);
                                                //view.performClick();

                                                getFragmentManager().beginTransaction().replace(R.id.mainactivity_framelayout, new HomeFragment()).commit();
                                            }
                                        });

And I didn't get any errors it just replaced without changing selected item. So I checked in my main activity that which item is checked when I tap

//First view
    getFragmentManager().beginTransaction().replace(R.id.mainactivity_framelayout, new HomeFragment()).commit();

    //bottom navi
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_home:
                    Log.d("checking bottom 1 = ",""+bottomNavigationView.getMenu().findItem(R.id.action_home).isChecked());
                    getFragmentManager().beginTransaction().replace(R.id.mainactivity_framelayout, new HomeFragment()).commit();
                    return true;
                case R.id.action_upload:
                    Log.d("checking bottom 2 = ",""+bottomNavigationView.getMenu().findItem(R.id.action_home).isChecked());
                    getFragmentManager().beginTransaction().replace(R.id.mainactivity_framelayout, new UploadFragment()).commit();
                    return true;
            }
            return false;
        }
    });

I tapped second case twice and first case twice and it shows as :

D/checking bottom 2 =: true
D/checking bottom 2 =: false
D/checking bottom 1 =: false
D/checking bottom 1 =: true

Am I missing something? I thought it supposed to show all true. I need your help! Thanks

bunbun
  • 2,595
  • 3
  • 34
  • 52
hwan
  • 5
  • 2

1 Answers1

0

I think the problem is that you check on a wrong BottomNavigationView, the following line in onCreateView is actually inflating a new instance of view without attaching it to the screen:

BottomNavigationView bottom = inflater.inflate(R.layout.activity_main, container, false).findViewById(R.id.mainactivity_bottom_navi);

Instead of inflating a new view, try access the view from its activity:

BottomNavigationView bottom = getActivity().findViewById(R.id.mainactivity_bottom_navi);
Aaron
  • 3,764
  • 2
  • 8
  • 25
  • wow thanks alot ! it works so well !! thank you ! you saved me! i spent so much time on it – hwan Oct 18 '18 at 12:59