onCreateOptionsMenu not called when starting new activity
Asked Answered
C

7

8

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.

Cosmogony answered 24/4, 2015 at 13:16 Comment(9)
Hi, What is your theme for activity?Gunny
@danny the theme for my entire application is android:theme="@style/Theme.AppCompat.Light"Cosmogony
Have you set any theme for second activity in manifest file?Brisling
Do you extend ActionBarActivity?Gunny
@Brisling Gupta I havent, but I just tried to set the theme to android:theme="@style/Theme.AppCompat.Light" and it still doesnt call onCreateOptionsMenuCosmogony
do you try my solution Errol Green ?Espagnole
@danny that was it danny, why is it that I have to extend ActionBarActivity instead of Activity?Cosmogony
@baroni, Yes I did baroni, just got it resolved, thanks thoCosmogony
@ErrolGreen Please see this answer: #26377805 Here you can fin explanation why it has not worked. It is connected with AppCompat library.Gunny
N
6

try adding method of super in your callback onCreateOptionsMenu implementation.

return super.onCreateOptionsMenu(menu);
Nunez answered 19/4, 2016 at 15:8 Comment(0)
A
9

In case you are using tool bar in your activity

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_map);
   mToolbar = (Toolbar) findViewById(R.id.tool_bar);
   setSupportActionBar(mToolbar);
}
Adagio answered 26/5, 2017 at 12:54 Comment(0)
N
6

try adding method of super in your callback onCreateOptionsMenu implementation.

return super.onCreateOptionsMenu(menu);
Nunez answered 19/4, 2016 at 15:8 Comment(0)
O
5

The problem in my case is if extend Activity, then onCreateOptionsMenu is not triggered. Did it manually with in onCreate:

Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.menu_main);

Of course, toolbar is defined in CoordinatorLayout

<android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
Obel answered 26/1, 2018 at 11:55 Comment(1)
after following this example learn.microsoft.com/en-us/xamarin/android/user-interface/… I found that your soltuion of inflating the menu on create is the only thing that seems to get the menu items to show. ThanksWithdrawal
E
1

don't forget to call parent function like this

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    ....
}
Espagnole answered 24/4, 2015 at 13:24 Comment(2)
are you calling return super.onOptionsItemSelected(item); inside onCreateOptionsMenu() or its just an illustration???Nunez
this will create a stackoverflow, a infinite loop of callsDrolet
E
0

you should building a new xml menu (Second.xml).

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);//Second es your new xml.
    return true;
}
Ebsen answered 24/4, 2015 at 13:44 Comment(0)
D
0

I was having the same problem: - I found the solution that worked for me.

1>Check to what you extend class: - In my case, I have tried my activity with extending AppCompatActivity class and the menu is showing proper and when I changed it to activity class then it stopped displaying.

Not Working: - public class GridViewActivity extends Activity

2>Try to extend your class with AppCompatActivity class instead of activity class.

Working: -

public class GridViewActivity extends AppCompatActivity

Deeply answered 5/5, 2017 at 11:19 Comment(0)
B
0

Call setHasOptionsMenu(true) from onCreate.

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
     }
Bucharest answered 29/8, 2017 at 13:31 Comment(1)
can you please elaborate and explain how this improves the already accepted answer ?Accommodating

© 2022 - 2024 — McMap. All rights reserved.