Search code examples
androidandroid-inflate

onCreateOptionsMenu not called when starting new activity


My onCreateOptionsMenu works only in my MainActivity and when I try to put another onCreateOptionsMenu in another activity to inflate a different menu it does not display my menu bar (note that I have it setup exactly the same in both activities).

I do not get any errors, it just does not display my menu bar on my secondary activity.

Here is my code:

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //noinspection SimplifiableIfStatement
    int id = item.getItemId();
    if (id == R.id.action_back) {
        finish();
    }
    return super.onOptionsItemSelected(item);
}

Also note that I changed getMenuInflater() to super.getMenuInflater() and got same result.


Solution

  • try adding method of super in your callback onCreateOptionsMenu implementation.

    return super.onCreateOptionsMenu(menu);