How to dismiss overflow menu when home button is pressed android?

669 views Asked by At

I am displaying overflow menu list when we click the three dots. When I press home button and again when I launch the app, overflow menu list is still displaying. How to dismiss overflow menu list window?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.v("RAMKUMARV ONCREATEOPTION", "RAMKUMARV");

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);

        // Inflate the menu; this adds items to the action bar if it is present.
        //  getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem item = menu.findItem(R.id.action_settings);
        item.setVisible(true);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Attached image

3

There are 3 answers

1
Zain On BEST ANSWER

You can have an activity field that stores the options/overflow menu whenever the onCreateOptionsMenu() gets triggered, and then use close() method to dismiss the menu when the home button is clicked i.e. in onUserLeaveHint().

public class MainActivity extends AppCompatActivity {

    // Field to store the overflow menu
    private Menu mOverflowMenu;

    // omitted rest of your code

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        mOverflowMenu = menu;
        
        // omitted rest of your code
        return true;
    }
    
    // Dismissing the overflow menu on home button click
    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
        mOverflowMenu.close();
    }   
    
}
4
Fates On

Declare the menu item like at MainActivity level, like this:

public class MainActivity extends AppCompatActivity {

    private MenuItem overflowItem;

    ...

}

Now you should instanciate the object inside the onCreateOptionsMenu method, like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    ...

    overflowItem = menu.findItem(R.id.action_settings);
    overflowItem.setVisible(true);
    return true;
}

Then you can use the onPause method to set the overflowItem not visible anymore:

@Override
public void onPause() {
    super.onPause();
    overflowItem.setVisible(false);
}

Finally remember to replace your MenuItem item object with overflowItem where it is used.

0
MariosP On

To dismiss the overflow menu you can simply call closeOptionsMenu() inside Activity.

closeOptionsMenu()

Progammatically closes the options menu. If the options menu is already closed, this method does nothing.

You can call it in onPause() method like below:

@Override
public void onPause() {
   super.onPause();
   closeOptionsMenu();
}

or in case you want to call it only when the user presses the Home key you can use onUserLeaveHint() like below:

@Override
protected void onUserLeaveHint() {
  super.onUserLeaveHint();
  closeOptionsMenu();
}