It would be great if I could specify which Activity to launch in the menu xml file, instead of having to override onOptionsItemSelected for every class that uses this method. It seems there could be something that could be done to implement this a bit more elegantly. Are there any solutions that can achieve this?

有帮助吗?

解决方案

I believe what you want is an ActionProvider.

You would specify an ActionProvider and that provider would implement whatever action you want such as launching an activity / sending an intent

其他提示

You could override onOptionsItemSelected in your base activity

public class MyActivity extends Activity {

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_launch_settings:
                //TODO Launch settings 
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Then in every menu.xml file that you want to launch Setting include the action_launch_settings item

<item 
    android:id="@+id/action_launch_settings"
    android:title="Launch Settings"
    />
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top