onMenuItemClick is not working in fragments
deepakcipl opened this issue · comments
Hi, Thanks for your wonderful code. I implement your code in fragment instead of activity. Everything is working fine but listeners are not working in fragment.
public class DinningFragment extends Fragment implements OnMenuItemClickListener{
@OverRide
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dinning, container, false);
}
}
@OverRide
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.settings_menu, menu);
this.menu = menu;
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.context_menu) {
if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
}
}
return super.onOptionsItemSelected(item);
}
@OverRide
public void onMenuItemClick(View clickedView, int position) {
MenuItem item = menu.findItem(R.id.context_menu);
Toast.makeText(this, "Clicked on position: " + position, Toast.LENGTH_SHORT).show();
}
}
Above is my code. I do not receive call in onMenuItemClick.
Please help!!
I suggest you to move the menu to the activity. Because either way you will have same toolbar in your activity.
Please go through the implementation description. You missed mMenuDialogFragment.setItemClickListener(this);
See changeLog Version: 1.0.5
Fixed menu item listener setting mechanism. It can be not activity but any class that implements listeners now. Issue. Attention! You have to set listeners to the context fragment manually. Check block 5 in the Usage.
Hello PenzK and others, I cannot wrap my head around getting past why I cannot click on the menu item and go to another activity. I read the Check Block 5, but confused where I need to add intent for each menu item. Do I add in Main Activity or the ContextMenuDialogFragment? Can you let me know what block of code in order to execute each menu item?
From Github Check Block 5 it shows Main Activity below, but has the "mMenuDialogFragment.setItemClickListener(this);"
* Implement OnMenuItemClickListener interface with onMenuItemClick method.*
public class MainActivity extends ActionBarActivity implements OnMenuItemClickListener
…
@OverRide
public void onMenuItemClick(View clickedView, int position) {
//Do something here
}
…
mMenuDialogFragment.setItemClickListener(this);
Any additional guidance is GREATLY appreciated. Thank you for your time.