I have created an action bar item for messages and when a new message is received it should be updated. The problem is when I refresh the activity it sometimes shows the icon as a new message and sometimes it shows as no new messages. It happens randomly. It is not updating correctly. I checked How to update a menu item shown in the ActionBar?
But couldn't fix my problem. I realize the problem is onCreateOptionMenu execute while oncreate. How can I delay that ?
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.loggedmenu, menu);
int no;
try {
no = Integer.parseInt(noOfmsgs);
} catch (NumberFormatException e) {
no = 0;
}
if (no > 0) {
menu.findItem(R.id.messages)
.setIcon(R.drawable.ic_action_new_email);
} else {
menu.findItem(R.id.messages).setIcon(R.drawable.ic_action_email);
}
return true;
}