[英]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). 我的onCreateOptionsMenu
仅适用于我的MainActivity,当我尝试将另一个onCreateOptionsMenu
放在另一个活动中以膨胀不同的菜单时,它不会显示我的菜单栏(请注意,我在两个活动中都设置了完全相同)。
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. 另请注意,我将getMenuInflater()
更改为super.getMenuInflater()
并获得了相同的结果。
尝试在回调onCreateOptionsMenu实现中添加super方法。
return super.onCreateOptionsMenu(menu);
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);
}
The problem in my case is if extend Activity, then onCreateOptionsMenu is not triggered. 我的问题是如果扩展Activity,则不会触发onCreateOptionsMenu。 Did it manually with in onCreate: 是否在onCreate中手动完成:
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.menu_main);
Of course, toolbar is defined in CoordinatorLayout 当然,工具栏是在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>
don't forget to call parent function like this 别忘了像这样调用父函数
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
....
}
you should building a new xml menu (Second.xml). 你应该构建一个新的xml菜单(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;
}
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. 1>检查你扩展类的内容: - 就我而言,我已经尝试了扩展AppCompatActivity类的活动,菜单显示正确,当我将其更改为活动类时,它就停止显示了。
Not Working: - public class GridViewActivity extends Activity 不工作: - 公共类GridViewActivity扩展Activity
2>Try to extend your class with AppCompatActivity class instead of activity class. 2>尝试使用AppCompatActivity类而不是活动类来扩展您的类。
Working: - 工作: -
public class GridViewActivity extends AppCompatActivity 公共类GridViewActivity扩展了AppCompatActivity
Call setHasOptionsMenu(true)
from onCreate
. 从onCreate
调用setHasOptionsMenu(true)
。
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.