BottomNavigationView is not gets checked when switching, Navigation component

Viewed 6

I am new to Navigation component library. Here I have two bottom navigation fragments: a ProfileFragment and a TrendsFragment. From TrendsFragment, I can open a new fragment called BlankFragment. The problem is when I do Trends-Blank-Profile-Trends, it navigates to Blank fragment but the BottomNavigation button remains unchecked. I do not want to BlankFragment be deleted the only problem is that the button remains unchecked. I am adding 3 pictures for your better understanding. trends-blank trends-blank-profile trends-blank-profile-trends

Your help will be really precious for me, thanks a lot in advance. here is my code:

MainActivity:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
ActivityMainBinding binding;
NavController navController;
AppBarConfiguration appBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    NavHostFragment navHostFragment=(NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_container);
    navController = navHostFragment.getNavController();
    BottomNavigationView bottomNavigationView=(BottomNavigationView)findViewById(R.id.forum_bottom_nav);
    HashSet<Integer> menuIds=new HashSet<Integer>();
    menuIds.add(R.id.blankFragment);
    menuIds.add(R.id.profileFragment);
    menuIds.add(R.id.trendsFragment);
    appBarConfiguration = new AppBarConfiguration.Builder(menuIds)
            .build();
    //navController = Navigation.findNavController(this, R.id.nav_host_fragment_container);

    NavigationUI.setupWithNavController(bottomNavigationView, navController);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
}

@Override
public boolean onSupportNavigateUp() {
    return navController.navigateUp();
}
}

navigation graph:

<?xml version="1.0" encoding="utf-8"?>
[enter image description here][1]<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/trendsFragment">
<fragment
    android:id="@+id/profileFragment"
    android:name="com.yusufemirbektas.bottomnavfrags.ProfileFragment"
    android:label="fragment_profile"
    tools:layout="@layout/fragment_profile" />

<fragment
    android:id="@+id/trendsFragment"
    android:name="com.yusufemirbektas.bottomnavfrags.TrendsFragment"
    android:label="fragment_trends"
    tools:layout="@layout/fragment_trends">
    <action
        android:id="@+id/action_trendsFragment_to_blankFragment"
        app:destination="@id/blankFragment" />
</fragment>

<fragment
    android:id="@+id/blankFragment"
    android:name="com.yusufemirbektas.bottomnavfrags.BlankFragment"
    android:label="fragment_blank"
    tools:layout="@layout/fragment_blank">
    <action
        android:id="@+id/action_blankFragment_self"
        app:destination="@id/blankFragment" />
</fragment>
</navigation>

if you would like to access the whole project, I put here a github link: https://github.com/personaljss/BottomNavfrags.git

0 Answers
Related