androidandroid-actionbaractionbarsherlocksettingsup-button

Navigation Up button not working in Preference Activity


I am using ActionBarSherlock and in my Preference Activity, as soon as I add the @Override onOptionsItemSelected function, I get an error.

The error - The method onOptionsItemSelected(MenuItem) of type SettingsActivity must override or implement a supertype method

Is this wrong?

public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);

        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    // Following method throws the error
    @Override
    public boolean onOptionsItemSelected(MenuItem item) { // <-- Error
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);  // <-- Error
    }

}

Solution

  • If you're using ABS, make sure you're using SherlockPreferenceActivity instead of PreferenceActivity. Also, be sure to import MenuItem from the right package.