简体   繁体   中英

Android Studio - onClick Fragment Transaction does not change Bottom Navigation Menu Icon Color

I am building an app which contains a Bottom Navigation Menu with 5 Fragments. Once the app launches, you are greeted with an Initial Screen, which acts as a place to initialize methods such as openFragment , onOptionsItemSelected , etc. Here, I initialize the Bottom Navigation Menu , using

bottomNavigation = findViewById(R.id.bottom_navigation);
bottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);

and the following method:

    BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @SuppressLint("NonConstantResourceId")
                @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.navigation_goals:
                            openFragment(GoalsFragment.newInstance("", ""));
                            return true;
                        case R.id.navigation_measure:
                            openFragment(MeasureFragment.newInstance("", ""));
                            return true;
                        case R.id.navigation_progress:
                            openFragment(ProgressFragment.newInstance("", ""));
                            return true;
                        case R.id.navigation_social:
                            openFragment(SocialFragment.newInstance("", ""));
                            return true;
                        case R.id.navigation_reflect:
                            openFragment(ReflectFragment.newInstance("", ""));
                            return true;
                    }
                    return false;
                }
            };

Everything works as it should, but I recently added a color changing selector, which changes the current Fragment's icon color on the Bottom Navigation menu. This is its code:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/app_purple" />
    <item android:state_checked="false" android:color="#808080"/>
</selector>

This works when I change items using the Bottom Navigation Menu. However, the problem is, I have got some Fragments that utilize the FragmentTransaction method to change Fragments upon clicking the "complete" button, such as the one below:

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gad_result_back:
                Fragment fr = new ProgressFragment();
                FragmentManager fm = getFragmentManager();
                FragmentTransaction fragmentTransaction = fm.beginTransaction();
                fragmentTransaction.replace(R.id.fg_gad_test_result_container, fr);
                fragmentTransaction.commit();
                break;
        }
    }

When clicking the button, the transaction is successful and I can see the new Fragment, but it is not set as the current one and thus, its navigation icon color does not change. I cannot use methods such as bottomNavigationView.setSelectedItemId , because the Navigation Menu is initialized when the app is launched, and I believe cannot be accessed from the Fragments directly.

In essence, I am looking for a way to let the app know that after the Fragment Transaction, we got a new Fragment as the active one, so change its color accordingly.

Any ideas?

For future visitors with the same issue, I have fixed it by adding the following method in the Initial Screen:

    public void setNavItem() {
        bottomNavigation = findViewById(R.id.bottom_navigation);
        bottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
        bottomNavigation.setSelectedItemId(R.id.navigation_progress);
    }

and then calling it from a onClick Fragment Transaction like this:

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gad_result_back:
                Fragment fr = new ProgressFragment();
                FragmentManager fm = getFragmentManager();
                FragmentTransaction fragmentTransaction = fm.beginTransaction();
                fragmentTransaction.replace(R.id.fg_gad_test_result_container, fr);
                fragmentTransaction.commit();
                ((InitialScreen)getActivity()).setNavItem();
                break;
        }
    }

This sets the icon color according to the Fragment Transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM