Search code examples
androidandroid-fragmentsandroid-actionbarmenu-items

Android: How to prevent the MenuItems in navigation drawer from invalidating?


I'm developing an android app, it uses a navigation drawer, it has only one Activity : the MainActivity, and the rest are all fragments.

Now the action bar has 3 Menu Items, one of them is a play button for a live radio stream and its drawable is a play image, when i press the button i have implemented a code in the MainActivity onOptionsItemSelected(MenuItem item)to change the button's drawable to a stop image.

Now when i navigate to another fragment using the Fragment Transaction; the live stream item returns to its initial state which is a play drawable. I think the Menu is recreating itself (i don't know).

How can i preserve the Menu Items state all the time ?

Thanks in advance.


Solution

  • Overriding onPrepareOptionsMenu might work. You could maintain a Boolean variable and check its state in onPrepareOptionsMenu to decide what should be the drawable for your menu item.