Android: How to change the selected item in Navigation Drawer onBackPressed()?

Fadi Obaji

I implemented a NavigationDrawer and when i press on a NavigationDrawer item a Fragment Transaction happens and the item's background changes to indicate that it's selected. Now when i press the Hardware Back Button to go to the previous Fragment the Navigation Drawer item's background remains. How can i change the selected item to refer to the current Fragment when `onBackPressed' occur ?

Thanks in advance.

Ashton Engberg

In your activity, override public void onBackPressed().

Give each fragment that comes into view a number that (conveniently) corresponds to the position in the navigation drawer item. Or save the last fragment's position. Then when onBackPressed() is fired, either from the fragment's "position" or "id" or "tag" you associated with its nav item row, you can use this to correctly colorize the now current nav item (its easy to just reset them all to default, and recolor in the "current" fragment). That's what I meant by the method: updateCurrentSelectedItemBackground() which resets the backgrounds, selecting the correct background for the current item and setting the rest to their default background.

Make sure to call super.onBackPressed() after you are done from within onBackPressed() so the system does it normal on back pressed behavior.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android: How to change the selected item in Navigation Drawer onBackPressed()?

From Dev

How to change selected Item in the navigation drawer depending on the activity/view?

From Dev

Navigation Drawer item remains selected Android

From Dev

Navigation drawer: How to know the current selected item?

From Dev

Navigation Drawer: How to change the colour of item category?

From Dev

Android Navigation drawer: How to change actionBarMenu according to drawer list item select

From Dev

How do I close a navigation drawer after an item is selected?

From Java

Navigation drawer: How do I set the selected item at startup?

From Dev

How do I close a navigation drawer after an item is selected?

From Dev

Navigation drawer is not closing while navigation item is selected

From Dev

Navigation Drawer item background colour for selected item

From Dev

How to change fragments using Android navigation drawer

From Dev

Android - How to change fragments in the Navigation Drawer

From Dev

How can I change the icon of the navigation drawer and its item android studio

From Dev

How to change color of icon of selected item in material drawer?

From Dev

How to Change the list selected item color in the navidgation drawer?

From Dev

How to Change the list selected item color in the navidgation drawer?

From Dev

Navigation drawer does not retain selected item

From Dev

Stay highlighted the selected item in navigation drawer

From Dev

Android - How to click on an item on a navigation drawer using Espresso?

From Dev

How to add a collapsible menu item inside navigation drawer in android?

From Java

Change the color of a checked menu item in a navigation drawer

From Dev

Change textsize of Drawer item android

From Dev

Change textsize of Drawer item android

From Dev

how to change the Title in Navigation Drawer

From Dev

How to change the Navigation Drawer Icon?

From Dev

How to change Navigation drawer launcher icon color in Android

From Dev

How to change the title arrow icon in android navigation drawer

From Dev

How do I change Item title font color for an Item that contains a menu in Navigation Drawer

Related Related

  1. 1

    Android: How to change the selected item in Navigation Drawer onBackPressed()?

  2. 2

    How to change selected Item in the navigation drawer depending on the activity/view?

  3. 3

    Navigation Drawer item remains selected Android

  4. 4

    Navigation drawer: How to know the current selected item?

  5. 5

    Navigation Drawer: How to change the colour of item category?

  6. 6

    Android Navigation drawer: How to change actionBarMenu according to drawer list item select

  7. 7

    How do I close a navigation drawer after an item is selected?

  8. 8

    Navigation drawer: How do I set the selected item at startup?

  9. 9

    How do I close a navigation drawer after an item is selected?

  10. 10

    Navigation drawer is not closing while navigation item is selected

  11. 11

    Navigation Drawer item background colour for selected item

  12. 12

    How to change fragments using Android navigation drawer

  13. 13

    Android - How to change fragments in the Navigation Drawer

  14. 14

    How can I change the icon of the navigation drawer and its item android studio

  15. 15

    How to change color of icon of selected item in material drawer?

  16. 16

    How to Change the list selected item color in the navidgation drawer?

  17. 17

    How to Change the list selected item color in the navidgation drawer?

  18. 18

    Navigation drawer does not retain selected item

  19. 19

    Stay highlighted the selected item in navigation drawer

  20. 20

    Android - How to click on an item on a navigation drawer using Espresso?

  21. 21

    How to add a collapsible menu item inside navigation drawer in android?

  22. 22

    Change the color of a checked menu item in a navigation drawer

  23. 23

    Change textsize of Drawer item android

  24. 24

    Change textsize of Drawer item android

  25. 25

    how to change the Title in Navigation Drawer

  26. 26

    How to change the Navigation Drawer Icon?

  27. 27

    How to change Navigation drawer launcher icon color in Android

  28. 28

    How to change the title arrow icon in android navigation drawer

  29. 29

    How do I change Item title font color for an Item that contains a menu in Navigation Drawer

HotTag

Archive