I tried both onCreateOptionsMenu(Menu menu) and onPrepareOptionsMenu(Menu menu) methods but when app opens in background I cant change icon visibility.As I think I need to call these methods inside the onResume()
When app start first time It work as expected.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it present.
getMenuInflater().inflate(R.menu.example_menu, menu);
// show manu items if not ofline mode
if (Utils.checkNetworkStatus(ExampleActivity.this)) {
menu.findItem(R.id.edit).setVisible(true);
menu.findItem(R.id.delete).setVisible(true);
}else {
menu.findItem(R.id.edit).setVisible(false);
menu.findItem(R.id.delete).setVisible(false);
}
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
if (Utils.checkNetworkStatus(ExampleActivity.this)) {
menu.findItem(R.id.edit).setVisible(true);
menu.findItem(R.id.delete).setVisible(true);
}else {
menu.findItem(R.id.edit).setVisible(false);
menu.findItem(R.id.delete).setVisible(false);
}
return true;
}
Thank you @jon and @Oliver Adam for your answers.
It directs me to the final solution. This is how i solved the problem with 100% accuracy.
According to the documentation if we need to change menu items at Runtime, It's recommended to use onPrepareOptionsMenu(Menu menu) method instead using onCreateOptionsMenu(Menu menu)
After the system calls onCreateOptionsMenu(), it retains an instance of the Menu you populate and will not call onCreateOptionsMenu() again unless the menu is invalidated for some reason. However, you should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity lifecycle.
If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)--Official Android Documentation--
And we need to call invalidateOptionsMenu() inside the onResume() method for refreash tha view when app comes from background.
@Override
protected void onResume() {
super.onResume();
invalidateOptionsMenu();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
if (Utils.checkNetworkStatus(ExampleActivity.this)) {
menu.findItem(R.id.edit).setVisible(true);
menu.findItem(R.id.delete).setVisible(true);
}else {
menu.findItem(R.id.edit).setVisible(false);
menu.findItem(R.id.delete).setVisible(false)
}
return true;
}
Follow this process if you need to edit your menu items dynamically:
Example:
private MenuItem editMenuItem, deleteMenuItem;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
editMenuItem = menu.findItem(R.id.edit).setVisible(true);
deleteMenuItem = menu.findItem(R.id.delete).setVisible(true);
return true;
}
@Override
protected void onResume() {
super.onResume();
boolean online = Utils.checkNetworkStatus(ExampleActivity.this);
if (editMenuItem != null){
editMenuItem.setVisible(online);
}
if (deleteMenuItem != null){
deleteMenuItem.setVisible(online);
}
invalidateOptionsMenu();
// or supportInvalidateOptionsMenu();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With