1

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

Shadow
  • 6,864
  • 6
  • 44
  • 93

3 Answers3

2

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();
    }   
    
}
Zain
  • 37,492
  • 7
  • 60
  • 84
  • 1
    Upvote for `onUserLeaveHint` because it's a more suitable function for what OP wants to achieve compared to `onPause`, an edge case can be activities which will be shown over lock screen and some devices might call `onPause` when trying to show unlock pattern for example – Amin Jun 19 '21 at 01:47
0

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.

Fates
  • 36
  • 4
0

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();
}
MariosP
  • 8,300
  • 1
  • 9
  • 30