action bar (Toolbar) on android has 3 options menu. Its working fine. But the problem is when ever i am uninstalling and reinstalling this duplication of the menu item is showing. Also the Title is also remains same, not changing. after sometimes, say if we close the app and restarts its working fine. I am totally confused and struck.
Here is the Code, MainActivity (OnCreateOptionsMenu):
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
MenuItem item = menu.findItem(R.id.action_wishlist);
wishlistCount = (RelativeLayout) MenuItemCompat.getActionView(item);
wishlistCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
setWishlistCount();
MenuItem cartitem = menu.findItem(R.id.action_cart);
cartCount = (RelativeLayout) MenuItemCompat.getActionView(cartitem);
cartCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
restoreToolbar();
return super.onCreateOptionsMenu(menu);
}
Here is my menu_main.xml:
<menu 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">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
<item
android:id="@+id/action_wishlist"
android:orderInCategory="300"
android:title="@string/wishlist"
app:actionLayout="@layout/actionbar_badge_layout"
android:icon="@drawable/ic_wishlist_dark"
app:showAsAction="always"></item>
<item
android:id="@+id/action_cart"
android:orderInCategory="300"
android:title="@string/cart"
app:actionLayout="@layout/actionbar_badge_cart_layout"
android:icon="@drawable/ic_cart_dark"
app:showAsAction="always"></item>
</menu>
Here is the Screenshot: 1: first time: enter image description here
2: normal: enter image description here
Note: The problem occurs when the application opens with opening navigation bar, else its working fine. On createoptionsmenu is called at all times, but still its not working. Anyone can help on this?
I have cleared it by myself, by changing the code from "onCreateOptionsMenu" to "OnPrepareOptionsMenu". Also it is recommended to call Super class before clearing or inflating the menu.
Thanks for all.