Android Drawable Constant States

|
I was playing around this evening with the MonoDroid.ActionBar to add some functionality to be able to change themes at run time. For this I had to be able to add new Drawable to be reused for each Action Bar Item. Apparently when you set multiple views BackgroundDrawable with the same Drawable instance, it shares it state between these views. So in my case my Drawable was defined in XML and was a item selector for a button so that a different Drawable can be shown for each of the states: pressed, focused and so on. Then when pressing the items in the Action Bar, triggered the states for some of the other Action Bar Items, so when pressing one button the the state of another button was triggered.

I browsed around the world wide web and found some solutions using the method Mutate() which essentially should remove the shared Constant State in the Drawable, but as it still was the same exact reference I passed along nothing changed. Hence I was forced to duplicate the Drawable:

view.SetBackgroundDrawable(
ItemBackgroundDrawable.GetConstantState().NewDrawable()
);

Above code creates a new Drawble each time I add an Item to the Action Bar and eliminates the problems with the visual state of the Item.