سؤال

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