I decoupled view from my Activity and I have a separate class for all UI related operations. One operation I want to implement there is to inflate menu for Action Bar. I try doing things like these, but none of it works.
fun displayMenu() {
rootView.toolbar.inflateMenu(R.menu.my_menu)
rootView.toolbar.setOnMenuItemClickListener { item ->
if (item.itemId == R.id.action_one) {
listener.onActionOne()
}
true
}
}
I tried this:
activity.menuInflater.inflate(R.menu.my_menu, rootView.toolbar.menu)
and this:
rootView.toolbar.inflateMenu(R.menu.my_menu)
But none of these gets the job done. How can I inflate this menu?
It was all my silly mistake.
activity.menuInflater.inflate(R.menu.my_menu, rootView.toolbar.menu)
Works perfectly fine. Just remember to call it during or after Activity.onCreateOptionMenu. Complete example to make it works is something like:
public class MyActivity extends AppCompatActivity {
@Inject
MyView myView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(myView.getRootView());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
myView.displayMenu();
return super.onCreateOptionsMenu(menu);
}
}
public class MyViewImpl implements MyView {
@Override
public void displayMenu() {
activity.getMenuInflater().inflate(
R.menu.categories_modification_menu,
rootView.findViewById(R.id.toolbar).getMenu()
)
}
}
I used displayMenu() from Activity.onCreate before... Sorry for wasting your time, especially I did not post this part as I thought it is irrelevant to the question...