Skip to content Skip to sidebar Skip to footer

How To Create Option Menu In Fragmentactivity?

Hi Below is the code I am using to create option menu in my FragmentActivity :- @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu

Solution 1:

Return item within Switch case like. ITs Work For me.

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // When the user clicks START ALARM, set the alarm.case R.id.start_action:
            alarm.setAlarm(this);
            returntrue;
        // When the user clicks CANCEL ALARM, cancel the alarm. case R.id.cancel_action:
            alarm.cancelAlarm(this);
            returntrue;

        default:
            returnsuper.onOptionsItemSelected(item);
      }
}

Solution 2:

Although this question is old but to close it here is what i believe OP is missing in the code

In the

onCreateOptionsMenu

return it with the super as super.onCreateOptionsMenu(menu);

and in the

onOptionsItemSelected

return it with the super as super.onOptionsItemSelected(item);

all the return type is boolean so you will know it worked correctly when it returns true. Its like simillar to super.onCreate(savedInstancestate).

Solution 3:

Change

returnfalse;

to

returnsuper.onOptionsItemSelected(item);

as

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // When the user clicks START ALARM, set the alarm.case R.id.start_action:
                alarm.setAlarm(this);
                returntrue;
            // When the user clicks CANCEL ALARM, cancel the alarm. case R.id.cancel_action:
                alarm.cancelAlarm(this);
                returntrue;
        }
        returnsuper.onOptionsItemSelected(item);
    }

Edit:

Also you have to add the following to your Fragment

setHasOptionsMenu(true);

Post a Comment for "How To Create Option Menu In Fragmentactivity?"