简体   繁体   中英

Why does the Android onCreateOptionsMenu method return super.onCreateOptionsMenu?

As I'm new to Android programming, I've encountered another little thing that I don't understand. Why does the onCreateOptionsMenu method below return super.onCreateOptionsMenu instead of just calling super.onCreateOptionsMenu (as it's done in the onCreate method)?

(This is from an Android tutorial .)

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

(I've found no duplicate question on StackOverflow. I'm probably asking a silly question or I'm just bad at searching.)

onCreate()的返回类型为void,而onCreateOptionsMenu()返回boolean,这就是返回的原因。

The super.onCreateOptionsMenu(menu): will execute any code that has to be executed for the options menu to work properly. The code you write adds extra functionality/ decides properties of the options menu.

您必须返回true才能显示菜单,如果返回false,则不会显示。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM