Reputation: 493
For some reason, my optionItemSelected is not being called when i click the button associated with it. The timber log is not called, but this appears in the logs the moment after i click the button:
D/ViewRootImpl@ba4d36e[POIHistoryFlowActivity]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@ba4d36e[POIHistoryFlowActivity]: ViewPostImeInputStage processPointer 1
Here is the code i have:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.activity_recent_destinations, menu);
Timber.d("Menu was inflated");
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_item_clear:
Timber.d("Clear button was clicked");
// clear the destinations
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In similar fragments, the parent's activity does not override either onCreateOptionsItem()
nor onOptionsItemSelected()
, but their associated buttons work while in this fragment they do not and after combing through everything with the debugger I'm at a loss as to what the root cause could be. Any help would me super appreciated!
update: activity_recent_destinations.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_action_item_clear"
android:title="@string/menu_item_title_clear"
app:actionLayout="@layout/action_item_clear"
app:showAsAction="always" />
</menu>
Upvotes: 1
Views: 1307
Reputation: 2958
You are using an "action layout" for this item. This is just going to act like a normal view and clicking on it won't trigger the onOptionsItemSelected
callback. You have to get a reference to the layout itself and add a click listener:
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.activity_recent_destinations, menu);
MenuItem item = menu.findItem(R.id.menu_action_item_clear);
View actionView = item.getActionView();
// Set the listener on the root view or any children if necessary
actionView.setOnClickListener(...);
}
Upvotes: 5