Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Menu icons when app comes from background (in onResume() method)

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;
} 
like image 721
Chanaka Fernando Avatar asked Sep 15 '25 10:09

Chanaka Fernando


2 Answers

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;
  }
like image 183
Chanaka Fernando Avatar answered Sep 17 '25 01:09

Chanaka Fernando


Follow this process if you need to edit your menu items dynamically:

  1. Get a reference to your menu items after you create the menu
  2. change the state of those items in your lifecycle method
  3. invalidate the menu

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();
}
like image 36
Jon Avatar answered Sep 17 '25 00:09

Jon