PenguinCake
PenguinCake

Reputation: 317

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.)

Upvotes: 5

Views: 4540

Answers (3)

tarn
tarn

Reputation: 556

You must return true for the menu to be displayed, if you return false it will not be shown.

Upvotes: 0

Eduard B.
Eduard B.

Reputation: 6803

onCreate()'s return type is void, while onCreateOptionsMenu() returns boolean, that's why the return.

Upvotes: 4

ChristianCuevas
ChristianCuevas

Reputation: 2692

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.

Upvotes: 2

Related Questions