Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving between fragments using BottomNavigationView

I am trying to use BottomNavigationView to move between 3 Fragments, and I made it work but I have a small issue.

I have 2 fragments and 1 activity, and this activity has the code moving between fragments in it.

When I press buttons on bottom navigation bar, I can see the image of bottom bar changes.

And in one fragment I made a button that will move to the other fragment when pressed, and it works well.

The issue I am facing is that when I press the button to move to the other fragment, the bottom bar image does not change.

Here is my code for the main activity:

public class MainActivity extends AppCompatActivity {


    public static Bundle bundle = new Bundle();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
        }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_announcement:
                        selectedFragment = new AnnouncementFragment();
                        break;
                }

                 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedFragment).commit();

                return true;
            }
        };
}

And this is the code of the fragment that has the button:

public class HomeFragment extends Fragment {

    Calendar calendar = Calendar.getInstance();
    Button buttonTrack;
    int year, month, day;

    public HomeFragment() {
        //Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);

        this.year = calendar.get(Calendar.YEAR);
        this.month = calendar.get(Calendar.MONTH) + 1;
        this.day = calendar.get(Calendar.DAY_OF_MONTH);

        buttonTrack = view.findViewById(R.id.btn_track);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
        final String currentTime = simpleDateFormat.format(new Date());


        buttonTrack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MainActivity.bundle.putString("time", currentTime);

                getFragmentManager().beginTransaction().replace(R.id.fragment_container, new AnnouncementFragment()).commit();

            }
        });

        return view;

    }
}

And the other fragment:

public class AnnouncementFragment extends Fragment {

    String timeFromBundle;
    TextView announcementHere, timeView;


    public AnnouncementFragment() {
    // Required empty public constructor
        }

    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_announcement, container, false);

        timeFromBundle = MainActivity.bundle.getString("time");

        announcementHere = view.findViewById(R.id.announcement_here);
        timeView = view.findViewById(R.id.time_view);
        timeView.setText(timeFromBundle);

        return view;
    }

}

Can anyone tell me when I should fix to make it changes the image in bottom bar even when I move to the other fragment by pressing a button inside a fragment? Thanks in advance!

like image 912
Auclown Avatar asked Oct 01 '18 09:10

Auclown


1 Answers

In your HomeFragment inside your button click listener instead of this line

getFragmentManager().beginTransaction().replace(R.id.fragment_container, new AnnouncementFragment()).commit();

use this

((BottomNavigationView)getActivity().findViewById(R.id.bottom_navigation)).setSelectedItemId(R.id.nav_announcement);

It will call your BottomNavigationView.OnNavigationItemSelectedListener in your activity.

like image 86
Vishal Avatar answered Nov 20 '22 20:11

Vishal