@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_example, menu); MenuItem item = menu.findItem(R.id.menu_item_example); Intent intent = new Intent(getApplicationContext(), ExampleActivity.class); item.setIntent(intent); return true; }
@Override public boolean onCreateOptionsMenu(Menu menu) { SubMenu subMenu = menu.addSubMenu("Options"); subMenu.add("Option 1").setIntent(Option1Intent()); subMenu.add("Option 2").setIntent(Option2Intent()); return true; } private Intent Option1Intent() { Intent intent = new Intent(getApplicationContext(), Option1Activity.class); return intent; } private Intent Option2Intent() { Intent intent = new Intent(getApplicationContext(), Option2Activity.class); return intent; }In this example, we add a sub-menu called "Options" to the menu. We then add two items to this sub-menu and associate intents with them using the setIntent method. These intents are created in separate methods for clarity. One intent navigates to Option1Activity, and the other navigates to Option2Activity. The package library for this method is android.view.