tomss
tomss

Reputation: 371

Navigation View - update item checked on back button press

I made a NavigationView on MainActivity (AppCompatActivity). I have multiple items on that NavigationView each one is a Fragment.

When I use back button I want to go to previous Fragment instead of put my app on background. Thus to change between Fragments I use:

       FragmentManager fragmentManager = getSupportFragmentManager();
       fragmentManager.beginTransaction().replace(R.id.layout, fragment).addToBackStack(null).commit();

When I press back button it goes to the previous Fragment, but the item checked of NavigationView remains. Which is the best way to update item checked of NavigationView on back button press?

Upvotes: 3

Views: 3131

Answers (4)

Victor Phan
Victor Phan

Reputation: 33

If you are using NavigationComponents, this is one of the ways

override fun onBackPressed() {
    super.onBackPressed()
    navController.currentBackStackEntry?.destination?.id?.let {
        binding.navView.setCheckedItem(it)
    }
}

Instead of using boiling code in every fragments, you can override this method in the mainActivity (fragment container).

Upvotes: 0

Alternatively, you can create a Stack, which stores the MenuItems of the NavigationDrawer when they are selected and then use the poll method in the onBackButtonPressed method to get and uncheck the MenuItem and the peek method to setChecked(true) the current MenuItem.

Probably not the most efficient way, but very easy to implement.

Upvotes: 0

dharmx
dharmx

Reputation: 1174

for future users here is another solution :

my drawer_menu.xml is like :

    <item
                android:id="@+id/Home"
                android:title="Home"
                android:icon="@drawable/ic_account_balance_black_24dp"
                >

            </item>
<item
            android:id="@+id/courses"
            android:title="courses"
            android:icon="@drawable/ic_airplay_black_24dp"
            >

        </item>
        <item
            android:id="@+id/download"
            android:title="Download"
            android:icon="@drawable/ic_file_download_black_24dp"
            >

        </item>

0 :home
1 :courses
2 :download

Now in your Mainactivity : (im showing you only navigation view purpose code)

private private NavigationView navigationView;

protected void onCreate(Bundle savedInstanceState) 
{
 navigationView= (NavigationView) findViewById(R.id.navview);

}

Now create a manual method in mainactivity to call this method from fragment when fragment resume:

 public void SetNavItemChecked(int id)
    {
        Menu m=navigationView.getMenu();
        MenuItem mi=m.getItem(id);
        mi.setChecked(true);
    }

Now in All fragments in this exmple Homefragment,CoursesFragment,DownloadFragments are the fragments

in HomeFragment (YourFragment) :

override onresume method :

    @Override
        public void onResume() {
            super.onResume();

            ((MainActivity)getActivity()).SetNavItemChecked(0);
            // MainActivity is your activity 
            //SetNavItemChecked is method from activity and 0 is first item in menu for this example o is HomeFragment
 }

and for all remaining fragment you can write in resume method like above

Upvotes: 1

Intimate
Intimate

Reputation: 456

i have used the following code into my project....and it worked.... I have inflated menu items into navigation drawer..... you have to implement it in your every fragment...in onActivityCreated() method....because this method will certainly called after activity is created so we can use UI elements...

//code.....

NavigationView navigation = (NavigationView)getActivity().findViewById(R.id.navigation_view);
Menu drawer_menu = navigation.getMenu();
MenuItem menuItem;
menuItem = drawer_menu.findItem(R.id.Id_ofYourMenuItem);
    if(!menuItem.isChecked())
    {
        menuItem.setChecked(true);
    }

So, every time your fragment is called(via navigation drawer or via pressing back button) ,it will set menu item checked itself.

Upvotes: 2

Related Questions