onCreateOptionsMenu() calling super

烈酒焚心 提交于 2019-11-29 21:20:45

It depends on what you want to do. First example will place your menu and only your menu. Second one, will add first super class menu. Last one will add your menu first. But, keep in mind that menus also have an order field, which will be taken into account at render time.

Let's say you are extending an activity that already has a menu, but you do not want that menu to appear but another one. In that case you would use first approach.

Another example: you are extending an activity that has a menu, and you want to add another menu. In that case you could use either second or last approach.

The source for onCreateOptionsMenu() is as follows:

public boolean onCreateOptionsMenu(Menu menu) {
    if (mParent != null) {
        return mParent.onCreateOptionsMenu(menu);
    }
    return true;
}

Where mParent is the parent Activity of the current Activity. If your Activity extends android.app.Activity then can just return true at the end and not worry about calling the super, as the default implementation will attempt to show a menu based on the parent Activity, which you probably don't want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!