Click here to Skip to main content
15,887,214 members
Home / Discussions / Android
   

Android

 
QuestionWhat is the correct syntax to call onCreateOptionsMenu() and onPrepareOptionsMenu() in Android Studio? Pin
priyamtheone2-Mar-24 4:39
priyamtheone2-Mar-24 4:39 
In Android Studio, I came across different ways to implement onCreateOptionsMenu() and onPrepareOptionsMenu() each with respect to the return statement in an Activity that extends from AppCompatActivity. It is quite confusing to me as to which one is the right way to implement the two methods, given that I want to maintain equality of syntax all across my application.

onCreateOptionsMenu()

Example 1: Only true is returned.

\@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

Example 2: Call to super class is returned.

\@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options_menu_activity_dashboard, menu);
    return super.onCreateOptionsMenu(menu);
}

Example 3: Call to super class is done in the first line and true is returned later.

\@Override
public boolean onCreateOptionsMenu(Menu menu){
    super.onCreateOptionsMenu(menu);
    Intent intent = new Intent(null, dataUri);
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);

    menu.addIntentOptions(
         R.id.intent_group,
         0,
         0,
         this.getComponentName(),
         null,
         intent,
         0,
         null);

    return true;
}

As you see, in the third example, call to super class is done followed by returning true. But in the first two examples, either true or call to super class is returned. Which one is the right way to do it?

onPrepareOptionsMenu()

Example 1: Call to super class is returned.

\@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (Build.VERSION.SDK_INT > 11) {
        invalidateOptionsMenu();
        menu.findItem(R.id.option2).setVisible(false);
        menu.findItem(R.id.option4).setVisible(true);
    }

    return super.onPrepareOptionsMenu(menu);
}

Example 2: Call to super class is done in the first line and true is returned later.

\@Override
public boolean onPrepareOptionsMenu(Menu menu) {
  super.onPrepareOptionsMenu(menu);
  menu.findItem(R.id.action_save).setVisible(true);
  menu.findItem(R.id.action_reset).setVisible(true);
  menu.findItem(R.id.action_about).setVisible(true);
  return true;
}

Here too the same question arises. Which syntax should I follow to implement the method properly?
AnswerRe: What is the correct syntax to call onCreateOptionsMenu() and onPrepareOptionsMenu() in Android Studio? Pin
Richard Deeming3-Mar-24 22:03
mveRichard Deeming3-Mar-24 22:03 
GeneralRe: What is the correct syntax to call onCreateOptionsMenu() and onPrepareOptionsMenu() in Android Studio? Pin
priyamtheone30-Mar-24 7:39
priyamtheone30-Mar-24 7:39 
QuestionWeb browser design home page Pin
Bibek Poudel Nov202319-Nov-23 20:32
Bibek Poudel Nov202319-Nov-23 20:32 
AnswerRe: Web browser design home page Pin
Dave Kreskowiak20-Nov-23 3:19
mveDave Kreskowiak20-Nov-23 3:19 
GeneralRe: Web browser design home page Pin
David Crow7-Feb-24 2:18
David Crow7-Feb-24 2:18 
QuestionThe user data that I enter are not insert into the Firebase Realtime Database. Pin
alia zulaika4-Oct-23 22:45
alia zulaika4-Oct-23 22:45 
QuestionBuilding an ecommerce app/website Pin
Ben A Johnson24-Apr-23 4:07
Ben A Johnson24-Apr-23 4:07 
AnswerRe: Building an ecommerce app/website Pin
Richard MacCutchan24-Apr-23 4:14
mveRichard MacCutchan24-Apr-23 4:14 
GeneralRe: Building an ecommerce app/website Pin
Ben A Johnson24-Apr-23 4:42
Ben A Johnson24-Apr-23 4:42 
GeneralRe: Building an ecommerce app/website Pin
Richard MacCutchan24-Apr-23 4:49
mveRichard MacCutchan24-Apr-23 4:49 
GeneralRe: Building an ecommerce app/website Pin
Ben A Johnson24-Apr-23 6:26
Ben A Johnson24-Apr-23 6:26 
GeneralRe: Building an ecommerce app/website Pin
Richard MacCutchan24-Apr-23 6:29
mveRichard MacCutchan24-Apr-23 6:29 
AnswerRe: Building an ecommerce app/website Pin
jschell25-Apr-23 11:07
jschell25-Apr-23 11:07 
GeneralRe: Building an ecommerce app/website Pin
Ben A Johnson25-Apr-23 20:47
Ben A Johnson25-Apr-23 20:47 
GeneralRe: Building an ecommerce app/website Pin
jschell26-Apr-23 6:10
jschell26-Apr-23 6:10 
GeneralRe: Building an ecommerce app/website Pin
Ben A Johnson27-Apr-23 23:32
Ben A Johnson27-Apr-23 23:32 
GeneralRe: Building an ecommerce app/website Pin
Richard MacCutchan27-Apr-23 23:35
mveRichard MacCutchan27-Apr-23 23:35 
GeneralRe: Building an ecommerce app/website Pin
Ben A Johnson28-Apr-23 1:55
Ben A Johnson28-Apr-23 1:55 
GeneralRe: Building an ecommerce app/website Pin
Richard MacCutchan28-Apr-23 2:02
mveRichard MacCutchan28-Apr-23 2:02 
GeneralRe: Building an ecommerce app/website Pin
jschell28-Apr-23 11:17
jschell28-Apr-23 11:17 
GeneralRe: Building an ecommerce app/website Pin
Richard MacCutchan28-Apr-23 20:55
mveRichard MacCutchan28-Apr-23 20:55 
GeneralRe: Building an ecommerce app/website Pin
David Crow29-Apr-23 11:48
David Crow29-Apr-23 11:48 
GeneralRe: Building an ecommerce app/website Pin
Richard MacCutchan29-Apr-23 22:03
mveRichard MacCutchan29-Apr-23 22:03 
GeneralRe: Building an ecommerce app/website Pin
Ben A Johnson2-May-23 2:10
Ben A Johnson2-May-23 2:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.