My fragment replaces the parent Activity options with a specific option item but when I click on the item, only activity's onOptionItemSelected gets called eventhough I've overridden the method inside Fragment. Am I missing something?
Fragment's methods:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "Fragment.onCreateOptionsMenu");
if (mPasteMode) {
menu.clear();
inflater.inflate(R.menu.contexual_paste, menu);
getActivity().getActionBar().setTitle("PasteMode");
}
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "Fragment.onOptionsItemSelected");
switch (item.getItemId()) {
case R.id.context_action_paste:
Toast.makeText(getActivity(),
"It worked ",
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Activity's methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "MainActivitiy.onOptionsItemSelected");
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Action Refresh selected", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
Logcat output:
MainActivity.onCreateOptionsMenu
Fragment.onCreateOptionsMenu
MainActivitiy.onOptionsItemSelected
So how can I have the onOptionsItemSelected of the fragment called?
If your Activity's onOptionsItemSelected method returs true, the call is consumed in activity and Fragment's onOptionsItemSelected is not called.
So, return false in your Activity onOptionsItemSelected method or parent class implementation via super.onOptionsItemSelected call (default implementation returns false).
According Activity class javadoc, method Activity.onOptionsItemSelected should:
Return false to allow normal menu processing to proceed, true to consume it here
You are not chaining to the superclass in the activity methods. Please have onCreateOptionsMenu() return super.onCreateOptionsMenu(menu), and have onOptionsItemSelected() return super.onOptionsItemSelected(item) (except for the item that you are handling, which should return true to indicate that you have handled the event).
In my case I did not add any menu items (i.e. I did not call onCreateOptionsMenu in either the activity or the fragment). However, I needed to use the action bar home (up) button in the fragment. For this I had to make sure that setHasOptionsMenu(true) was called in the fragment's onCreateView() method before this could work. Then I didn't need an onOptionsItemSelected override in my activity.
I found solution i.e
Fragment.class
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Do something that differs the Activity's menu here
//MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.wifinity_setting, menu);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString());
spanString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, spanString.length(), 0); //fix the color to white
item.setTitle(spanString);
}
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
Intent intent3 = new Intent(context, activity.class);
startActivity(intent3);
return true;
}
return true;
}
ACtivity.class
overide the onOptionsItemSelected()
// fragmnets onOptionselected method get called ..
this solution works for me
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
default:
if(fragment != null)
fragment.onOptionsItemSelected(item);
}
return true;
}
I agree with the currently accepted solution, but another possible cause is having an ambiguous class reference. I had a custom class in my project named MenuItem and my Fragment was interpreting that custom.MenuItem as the parameter type instead of android.view.MenuItem
The symptoms were a wiggly red underline on my Override and IDE message indicating that onOptionsItemSelected will not be called.
After a few hours of working on this issue, I found a solution.
You need to call onOptionsItemSelected in activity but return false like this
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return false
}
Next step is adding setHasOptionsMenu(true) inside fragment onCreate()
And finally you can call onOptionsItemSelected in your fragment and handle menu item click
override fun onOptionsItemSelected(item: MenuItem): Boolean {
//My awesome stuff
return super.onOptionsItemSelected(item)
}
That's it.
Related
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//no inspection SimplifiableIfStatement
if (id == R.id.action_filter) {
FragmentManager fm = getSupportFragmentManager();
if (userType.equals("İş Arayan"))
filterDialogTitle = "İş İlanları Filtre";
else if (userType.equals("Hizmet Arayan"))
filterDialogTitle = "Hizmet İlanları Filtre";
FilterDialogFragment editNameDialogFragment = FilterDialogFragment.newInstance(filterDialogTitle);
editNameDialogFragment.show(fm, "fragment_edit_name");
return true;
}
return super.onOptionsItemSelected(item);
}
I added in Fragment, but it didn' t got called, if i add in MainActivity, it works but i want to call it in Fragment. How can i do this ?
In Fragment you have to call setHasOptionsMenu(true)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
...
}
Then suppose you have to handle menu_item_to_handle_in_fragment item click
For Fragment class
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_to_handle_in_fragment:
// Do onlick on menu action here
return true;
}
return false;
}
For Activity class
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_to_handle_in_fragment:
return false;
}
return false;
}
You need to add setHasOptionMenu(true) in your onCreate of fragment.
When you add this option the fragment lifecycle will calls the onCreateOptionMenu() and onOptionItemSelected().
Follow this steps:
Add setHasOptionsMenu(true) method in onCreate() of your Fragment.
Override onCreateOptionsMenu(Menu menu, MenuInflater inflater) and
onOptionsItemSelected(MenuItem item) methods in your Fragment.
Inside your onOptionsItemSelected(MenuItem item) Activity's method,
make sure you return false when the menu item action would be
implemented in onOptionsItemSelected(MenuItem item) Fragment's
method.
Steps to create Option Menu in fragment
1.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_home, menu);
super.onCreateOptionsMenu(menu, inflater);
}
2.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
//call function as per your requirement
return true;
default:
return false;
}
}
Ok, many answers and none of the above shows how to actually call Frarment.
This is the whole example tested and working.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//activate menu on the top to show icons or menu
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
//this part we need to show back arrow on top so user could use backstack
setupActionBarWithNavController(findNavController(R.id.fragmentContainer))
//setupActionBarWithNavController(findNavController(R.id.fragmentContainer))
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragmentContainer)
return navController.navigateUp() || super.onSupportNavigateUp()
}
//show icons in top menu
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
//inflate our menu our menu situated in res menu menu.xml so R.menu.menu(our menu)
menuInflater.inflate(R.menu.menu, menu)
return true
}
//if item in menu selected do ......
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val navController = findNavController(R.id.fragmentContainer)
when (item.itemId) {
**R.id.iconAbout -> navController.navigate(R.id.aboutFragment)**
}
return super.onOptionsItemSelected(item)
}
}
So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
case R.id.menuregistrar:
break;
case R.id.menusalir:
break;
}
return true;
}
But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.
Put this method in your Activity
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem register = menu.findItem(R.id.menuregistrar);
if(userRegistered)
{
register.setVisible(false);
}
else
{
register.setVisible(true);
}
return true;
}
in shorter version you could write:
MenuItem register = menu.findItem(R.id.menuregistrar);
register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
return true;
I would simplify Adil's solution even further with the following:
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem registrar = menu.findItem(R.id.menuregistrar);
registrar.setVisible(!isRegistered);
return true;
}
Simply do one thing get the id of the item of menu from this line:
Menu menu =navigationView.getMenu();
MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);
and than make it visible it accourding to you by this line:
nav_dashboard.setVisible(true/false);
Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//
Example
private Menu menu_change_language;
...
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
...
menu_change_language = menu;
...
...
return super.onCreateOptionsMenu(menu);
}
use code below for hiding Menu Item:
if(menu_change_language != null){
menu_change_language.findItem(R.id.menu_change_language)
.setVisible(false);
}
Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.
Cheers
If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setVisible(true);
return true;
}
OR
for item in the menu that you didn't click on
private Menu globalMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu."menu Xml Name", menu);
globalMenuItem = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
return true;
}
EDIT:
This question was for the deprecated sherlock action bar. Android support library should be used instead now
I have added an action bar menu option called share for my fragment which appears but the selection event is not being caught
I am adding it like this
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
MenuItem item = menu.add(0, 7,0, R.string.share);
item.setIcon(R.drawable.social_share).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
Trying to capture it in both the fragment and the fragment activity like
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 7:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "I'm being sent!!");
startActivity(Intent.createChooser(share, "Share Text"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and I have setHasOptionsMenu(true); in the onCreate().
Same problems happened to me:
onMenuItemSelected events didn't get called in Fragment
Searched google cann't find a solution, and add onMenuItemSelected method in FragmentActivity doesn't solve it.
Finally resolve it by following reference to http://developer.android.com/guide/topics/ui/actionbar.html
Note: If you added the menu item from a fragment, via the Fragment class's onCreateOptionsMenu callback, then the system calls the respective onOptionsItemSelected() method for that fragment when the user selects one of the fragment's items. However the activity gets a chance to handle the event first, so the system calls onOptionsItemSelected() on the activity before calling the same callback for the fragment.
Which means only if you don't have that menu item handler in onOptionsItemSelected() on the activity, the onOptionsItemSelected() on the fragment will be called.
Code as following
-----Remove the handler for R.action.add on FragmentActivity):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
popBackStack();
return true;
case R.id.action_search:
searchAction();
return true;
case R.id.action_logout:
userLogout();
return true;
//case R.id.action_add:
//return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the handler for R.action.add on Fragment looks like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("onOptionsItemSelected","yes");
switch (item.getItemId()) {
case R.id.action_add:
add();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Finally, remember to add
setHasOptionsMenu(true);
in your onCreate method in Fragment
I had the same problem, but I think it's better to summarize and introduce the last step to get it working:
Add setHasOptionsMenu(true) method in your Fragment's onCreate(Bundle savedInstanceState) method.
Override onCreateOptionsMenu(Menu menu, MenuInflater inflater) (if you want to do something different in your Fragment's menu) and onOptionsItemSelected(MenuItem item) methods in your Fragment.
Inside your Activity's onOptionsItemSelected(MenuItem item) method, make sure you return false when the menu item action would be implemented in Fragment's onOptionsItemSelected(MenuItem item) method.
An example:
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.activity_menu_item:
// Do Activity menu item stuff here
return true;
case R.id.fragment_menu_item:
// Not implemented here
return false;
default:
break;
}
return false;
}
Fragment
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
....
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Do something that differs the Activity's menu here
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.activity_menu_item:
// Not implemented here
return false;
case R.id.fragment_menu_item:
// Do Fragment menu item stuff here
return true;
default:
break;
}
return false;
}
I have noticed that the solution people gave you was to implement the code for your menue item in the activity rather then the fragment.
I think it will look much more orgenized if you had implemented the code in the fragment rather then the activity 'cos in my opinion it looks better.
To do so, do as follows :
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.SomeIDInTheMenueOfTheActivity:
{
//something();
break;
}
default:
//do something default and add the code under :
return super.onOptionsItemSelected(item);
}
return true;
}
Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.SomeIDInFragmentMenue:
{
break;
}
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Now the lines (and the likes): "return super.onOptionsItemSelected(item);" in the activity and fragment are super important, because as if you will follow the code in debug, you will see that the menue events functions will be called first on the Activity, and if the item did not match the id's in the activity's switch-case, the degault line : "super.onOptionsItemSelected(item);" will call the onOptionsItemSelected function on the fragment, as we wanted.
(if you have many fragments, make sure to have that line in them as well, as the calling hirarchy can be somewhat complicated).
I'm using actionbarsherlock. This worked for me:
1) Create dummy_menu.xml menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="fill_parent" >
<item
android:title=""
android:showAsAction="never"
android:id="#+id/dummyMenu"
/>
2) In activity inflate the menu like this:
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.dummy_menu,menu);
return super.onCreateOptionsMenu(menu);
}
3) In fragments onCreateView call setHasOptionsMenu(true) and override onCreateOptionsMenu and onOptionsItemSelected also hide the dummyMenu like this (in fragment)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_actions, menu);
MenuItem item = menu.findItem(R.id.dummyMenu);
item.setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
Hope it helps someone.
Edit for actionbar sherlock use
I had to use
public boolean onMenuItemSelected(int featureId, MenuItem item) {
in the main activity to capture the menu item
it's so simple you can do that in your fragment to make sure that your action will listen correctly:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
From the documentation, it should solve by this steps:
If you in Activity Using the addMenuProvider() API directly:
class ExampleActivity : ComponentActivity(R.layout.activity_example) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Add menu items without overriding methods in the Activity
addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
// Add menu items here
menuInflater.inflate(R.menu.example_menu, menu)
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
// Handle the menu selection
return true
}
})
}
}
If you in Fragment Using the addMenuProvider() API
class ExampleFragment : Fragment(R.layout.fragment_example) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// The usage of an interface lets you inject your own implementation
val menuHost: MenuHost = requireActivity()
// Add menu items without using the Fragment Menu APIs
// Note how we can tie the MenuProvider to the viewLifecycleOwner
// and an optional Lifecycle.State (here, RESUMED) to indicate when
// the menu should be visible
menuHost.addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
// Add menu items here
menuInflater.inflate(R.menu.example_menu, menu)
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
// Handle the menu selection
return true
}
}, viewLifecycleOwner, Lifecycle.State.RESUMED)
}
I had this problem. It was because I was overiding the wrong method
onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
is what I used.
Make sure you are using the right one!
You are not chaining to the superclass in the activity methods. Please have onCreateOptionsMenu() return super.onCreateOptionsMenu(menu), and have onOptionsItemSelected() return super.onOptionsItemSelected(item) (except for the item that you are handling, which should return true to indicate that you have handled the event)
you must add this code toolbar.bringToFront(); next set toolbar in your activity
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
...
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Yazd");
setSupportActionBar(toolbar);
toolbar.bringToFront(); // <<= add here
...
So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
case R.id.menuregistrar:
break;
case R.id.menusalir:
break;
}
return true;
}
But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.
Put this method in your Activity
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem register = menu.findItem(R.id.menuregistrar);
if(userRegistered)
{
register.setVisible(false);
}
else
{
register.setVisible(true);
}
return true;
}
in shorter version you could write:
MenuItem register = menu.findItem(R.id.menuregistrar);
register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
return true;
I would simplify Adil's solution even further with the following:
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem registrar = menu.findItem(R.id.menuregistrar);
registrar.setVisible(!isRegistered);
return true;
}
Simply do one thing get the id of the item of menu from this line:
Menu menu =navigationView.getMenu();
MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);
and than make it visible it accourding to you by this line:
nav_dashboard.setVisible(true/false);
Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//
Example
private Menu menu_change_language;
...
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
...
menu_change_language = menu;
...
...
return super.onCreateOptionsMenu(menu);
}
use code below for hiding Menu Item:
if(menu_change_language != null){
menu_change_language.findItem(R.id.menu_change_language)
.setVisible(false);
}
Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.
Cheers
If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setVisible(true);
return true;
}
OR
for item in the menu that you didn't click on
private Menu globalMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu."menu Xml Name", menu);
globalMenuItem = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
return true;
}
My XML menu definition sets the item R.id.menu_refresh's enabled state to false. When the app runs the menu item is greyed and disabled. Why is this code in the app not enabling the item?
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem refresh = menu.getItem(R.id.menu_refresh);
refresh.setEnabled(true);
return true;
}
What am I missing?
Try menu.findItem() instead of getItem(). getItem() takes an index from [0, size) while findItem() takes an id.
this is what I do in my activity for the menu handling ...
//Android Activity Lifecycle Method
// This is only called once, the first time the options menu is displayed.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
//Android Activity Lifecycle Method
// Called when a panel's menu is opened by the user.
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
MenuItem mnuLogOut = menu.findItem(R.id.main_menu_log_out_id);
MenuItem mnuLogIn = menu.findItem(R.id.main_menu_log_in_id);
MenuItem mnuOptions = menu.findItem(R.id.main_menu_options_id);
MenuItem mnuProfile = menu.findItem(R.id.main_menu_profile_id);
//set the menu options depending on login status
if (mBoolLoggedIn == true)
{
//show the log out option
mnuLogOut.setVisible(true);
mnuLogIn.setVisible(false);
//show the options selection
mnuOptions.setVisible(true);
//show the edit profile selection
mnuProfile.setVisible(true);
}
else
{
//show the log in option
mnuLogOut.setVisible(false);
mnuLogIn.setVisible(true);
//hide the options selection
mnuOptions.setVisible(false);
//hide the edit profile selection
mnuProfile.setVisible(false);
}
return true;
}
//Android Activity Lifecycle Method
// called whenever an item in your options menu is selected
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.main_menu_log_in_id:
{
ShowLoginUI();
return true;
}
case R.id.main_menu_log_out_id:
{
ShowGoodbyeUI();
return true;
}
case R.id.main_menu_options_id:
{
ShowOptionsUI();
return true;
}
case R.id.main_menu_profile_id:
{
ShowProfileUI();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
I like this approach because it makes the code nice and modular
Use menu.findItem() instead of getItem().
because findItem is used to find item by id.